inline void Vector2::clamp(float length) { if (squaredLength() > square(length)) { normalize(); *this *= length; } }
inline float Vector2::normalize() { float f = squaredLength(); if (f != 0.0f && f != 1.0f) { *this /= std::sqrt(f); } return f; }
Vector3 Vector3::normalizeVector() { float length = sqrt(squaredLength()); if (length == 0) { return *this; } return Vector3(x/length, y/length, z/length); }
int Sphere::testRay(const sf::Vector3f& origin, const sf::Vector3f& direction, sf::Vector3f& hitPosition, sf::Vector2f& UVCoordinates) { float factor = -dot((origin-transform.position), direction); sf::Vector3f x = origin + factor * direction; float distance = sqrt((transform.scale.x/2 * transform.scale.x/2) - squaredLength(x-transform.position)); if(factor > distance) { hitPosition = origin + (factor - distance) * direction; sf::Vector2f UVs(0.0f, 0.0f); if(material.texture) { sf::Vector3f yAxis(0.0f, 1.0f, 0.0f); sf::Vector3f zAxis(0.0f, 0.0f, -1.0f); sf::Vector3f normal = getNormal(hitPosition); float phi = acos( -dot( yAxis, normal )); UVs.y = phi / 3.14f; float theta = (acos( dot( normal, zAxis) / sin( phi ))) / ( 2 * 3.14f); if ( dot(cross( yAxis, zAxis), normal ) > 0 ) { UVs.x = theta; } else { UVs.x = 1 - theta; } } UVCoordinates = UVs; return 1; } else if(factor + distance > 0) { hitPosition = origin + (factor + distance) * direction; return -1; } else { return 0; } }
Circle computeCircumcircle(const AdvancedTriangle& triangle) { assert(at(triangle, 0) != at(triangle, 1) && at(triangle, 0) != at(triangle, 2)); // Compute midpoint of two sides sf::Vector2f p = 0.5f * (at(triangle, 0) + at(triangle, 1)); sf::Vector2f q = 0.5f * (at(triangle, 0) + at(triangle, 2)); // Compute perpendicular bisectors of the sides sf::Vector2f v = perpendicularVector(p - at(triangle, 0)); sf::Vector2f w = perpendicularVector(q - at(triangle, 0)); // Now we have the lines p + s*v and q + t*w with s and t being real numbers. The intersection is: sf::Vector2f intersection( v.x * (p.y * w.x + q.x * w.y - q.y * w.x) - p.x * v.y * w.x, w.y * (p.y * v.x + q.x * v.y - p.x * v.y) - q.y * v.y * w.x); intersection /= v.x * w.y - v.y * w.x; // Alternative to calculating intersection (slower): // sf::Vector3f cross = crossProduct(v,w); // sf::Vector2f intersection = p + v * dotProduct(crossProduct(q-p, w), cross) / squaredLength(cross); return Circle(intersection, squaredLength(intersection - at(triangle, 0))); }
T length() const { return std::sqrt(squaredLength()); }
/** Normalize the vector to have unit length, using a fast approximation to the reciprocal of the square root function. */ void fastUnitize() { *this *= Math::fastRsq(squaredLength()); }
/** Get a unit vector along the same direction, using a fast approximation to the reciprocal of the square root function. */ VectorT fastUnit() const { return *this * Math::fastRsq(squaredLength()); }
/** Get the length (L2-norm) of the vector, using a fast approximation to the square root function. */ T fastLength() const { return static_cast<T>(Math::fastSqrt(squaredLength())); }
/** Get the length (L2-norm) of the vector. */ T length() const { return static_cast<T>(std::sqrt(squaredLength())); }
void Mesh::fixTangentData(IntermediateMeshData& data) { if (data.uv0.size() == 0) { return; } uint32 numTris = data.position.size() / 3; for (uint32 i = 0; i < numTris; ++i) { uint32 idx[3] = {0}; idx[0] = i*3+0; idx[1] = i*3+1; idx[2] = i*3+2; const Vector3& edgeA = data.position[idx[1]] - data.position[idx[0]]; const Vector3& edgeB = data.position[idx[2]] - data.position[idx[0]]; const Vector2& uv0 = data.uv0[idx[0]]; const Vector2& uv1 = data.uv0[idx[1]]; const Vector2& uv2 = data.uv0[idx[2]]; float s1 = uv1.x - uv0.x; float t1 = uv0.y - uv1.y; float s2 = uv2.x - uv0.x; float t2 = uv0.y - uv2.y; float det = 1.0f / (s1 * t2 - s2 * t1); if (fabs(det) <= EPSILON) { det = 1.0f; } Vector3 tangent = Vector3((t2*edgeA.x - t1*edgeB.x) * det, (t2*edgeA.y - t1*edgeB.y) * det, (t2*edgeA.z - t1*edgeB.z) * det); Vector3 bitangent = Vector3((s1*edgeB.x - s2*edgeA.x) * det, (s1*edgeB.y - s2*edgeA.y) * det, (s1*edgeB.z - s2*edgeA.z) * det); Vector3 normal = cross(tangent, bitangent); normalize(tangent); normalize(bitangent); normalize(normal); Vector3 surfaceNormal = cross(edgeA, edgeB); normalize(surfaceNormal); bool needFixTB = dot(surfaceNormal, normal) < 0; for (uint32 vertex = 0; vertex < 3; ++vertex) { Vector3 vertexTangent = Vector3(data.tangent[idx[vertex]]); Vector3 vertexBitangent = data.bitangent[idx[vertex]]; float handeness = 1.0f; if (fabs(squaredLength(vertexTangent)) < EPSILON) { vertexTangent = tangent; } else { if (needFixTB) { if (dot(vertexTangent, tangent) < 0) { vertexTangent = -vertexTangent; } else { handeness = -1.0f; } } } // FIXME: handeness data.tangent[idx[vertex]] = vertexTangent; } } }
size_t WobbleNodeAnimator::animate( bool _update, size_t _quad_count, MyGUI::VectorQuadData& _data, float _time, MyGUI::IVertexBuffer* _buffer, MyGUI::ITexture* _texture, const MyGUI::RenderTargetInfo& _info, const MyGUI::IntCoord& _coord, bool& _isAnimate ) { if (mDestroy) { return _quad_count; } // проверяем смещения виджета if (mOldCoord.empty()) { mOldCoord = _coord; } else if (mOldCoord.size() != _coord.size() && mOldCoord.point() != _coord.point()) { mInertiaPoint.set(0.5, 0.5); mInertiaMode = false; addInertia(MyGUI::FloatPoint(_coord.left-mOldCoord.left, _coord.top-mOldCoord.top)); } else if (mOldCoord.size() != _coord.size()) { mInertiaMode = true; addInertia(MyGUI::FloatPoint(_coord.width - mOldCoord.width, _coord.height-mOldCoord.height)); } else if (mOldCoord.point() != _coord.point()) { const MyGUI::IntPoint& point = MyGUI::InputManager::getInstance().getMousePosition(); mInertiaPoint = MyGUI::FloatPoint((float)(point.left - _coord.left) / (float)_coord.width , (float)(point.top - _coord.top) / (float)_coord.height); mInertiaMode = false; addInertia(MyGUI::FloatPoint(_coord.left-mOldCoord.left, _coord.top-mOldCoord.top)); } mOldCoord = _coord; addTime(_time); bool anim_update = squaredLength(mDragOffset) >= 0.3f; if (!anim_update) { return _quad_count; } _isAnimate = true; _quad_count = tesselation( _quad_count, _data, _texture, _info, _coord); buildQuadVertex(_data); return _quad_count; }
float Vector::length() const { return sqrt(squaredLength()); }
Vector4 Vector4::normalizeVector() { float length = sqrt(squaredLength()); return Vector4(x/length, y/length, z/length, w/length); }
double Vector::length() const { return sqrtl(squaredLength()); }
inline float Vector2::length() const { return std::sqrt(squaredLength()); }