Skip to content

Commit

Permalink
- comments and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
vwaurich authored and adeas31 committed Sep 30, 2016
1 parent a144be8 commit f03fd1f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 36 deletions.
138 changes: 106 additions & 32 deletions OMEdit/OMEditGUI/Animation/ExtraShapes.cpp
Expand Up @@ -35,6 +35,10 @@
#include "ExtraShapes.h"
#include <iostream>

/*!
* \brief Pipecylinder::Pipecylinder
* creates a pipe or pipecylinder geometry
*/
Pipecylinder::Pipecylinder(float rI, float rO, float l) :
osg::Geometry()
{
Expand Down Expand Up @@ -131,11 +135,25 @@ Pipecylinder::Pipecylinder(float rI, float rO, float l) :
}
}

/*!
* \brief Spring::normalize
* \param the input vector
* normalizes a vector
* \return the normalized vector
*
*/
osg::Vec3f Spring::normalize(osg::Vec3f vec) {
float abs = absoluteVector(vec);
return osg::Vec3f(vec[0]/abs, vec[1] / abs, vec[2] / abs);
}

/*!
* \brief Spring::getNormal
* \param the input vector
* \param the length of the out vector
* gets an arbitrary normal to the input vector
* \return the normal
*/
osg::Vec3f Spring::getNormal(osg::Vec3f vec, float length) {
osg::Vec3f vecN = normalize(vec);
osg::Vec3f vecN_Abs = osg::Vec3f(std::abs(vecN[0]), std::abs(vecN[1]), std::abs(vecN[2]));
Expand Down Expand Up @@ -169,49 +187,91 @@ osg::Vec3f Spring::getNormal(osg::Vec3f vec, float length) {
return n;
}

/*!
* \brief Spring::absoluteVector
* \param the input vector
* gets the length of a vector
* \return the length
*/
float Spring::absoluteVector(osg::Vec3f vec)
{
return std::sqrt(std::pow(vec[0], 2) + std::pow(vec[1], 2) + std::pow(vec[2], 2));
}

/*!
* \brief Spring::angleBetweenVectors
* \param vector1
* \param vector2
* gets the angle between 2 vectors
* \return the angle
*/
float Spring::angleBetweenVectors(osg::Vec3f vec1, osg::Vec3f vec2)
{
float scalarProduct = vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2];
return (std::acos(scalarProduct/(absoluteVector(vec1)*absoluteVector(vec2)))/* / M_PI * 180*/);
}

/*!
* \brief Spring::rotateX
* \param vector1
* \param angle
* rotates the vector around the cartesian x axis with angle phi
* \return the rotated vector
*/
osg::Vec3f Spring::rotateX(osg::Vec3f vec, float phi)
{
return osg::Vec3f( vec[0],
vec[1] * std::cos(phi) - vec[2] * std::sin(phi),
vec[1] * std::sin(phi) + vec[2] * std::cos(phi));
}

/*!
* \brief Spring::rotateY
* \param vector1
* \param angle
* rotates the vector around the cartesian y axis with angle phi
* \return the rotated vector
*/
osg::Vec3f Spring::rotateY(osg::Vec3f vec, float phi)
{
return osg::Vec3f( vec[2] * std::sin(phi) + vec[0] * std::cos(phi),
vec[1],
vec[2] * std::cos(phi) - vec[0] * std::sin(phi));
}

/*!
* \brief Spring::rotateZ
* \param vector1
* \param angle
* rotates the vector around the cartesian z axis with angle phi
* \return the rotated vector
*/
osg::Vec3f Spring::rotateZ(osg::Vec3f vec, float phi)
{
return osg::Vec3f( vec[0] * std::cos(phi) - vec[1] * std::sin(phi),
vec[0] * std::sin(phi) + vec[1] * std::cos(phi),
vec[2]);
}

osg::Vec3f Spring::rotateArbitraryAxes_expensive(osg::Vec3f vec, osg::Vec3f axes, float phi)
/*!
* \brief Spring::rotateArbitraryAxis_expensive
* \param vector1
* \param rotation axis
* \param angle
* rotates the vector around the given axis with angle phi, this is a bit odd, use rotateArbitraryAxis
* \return the rotated vector
*/
osg::Vec3f Spring::rotateArbitraryAxis_expensive(osg::Vec3f vec, osg::Vec3f axis, float phi)
{
//this is how I would do it by hand. Check out rotateArbitraryAxes, thats the shortest formula.
//this is how I would do it by hand. Check out rotateArbitraryAxis, thats the shortest formula.
//There is also still something wrong in here
osg::Vec3f axesN = normalize(axes);
osg::Vec3f axisN = normalize(axis);
osg::Vec3f aux = vec;
//angle between vec and x, rotate in xz-plane
float phiX = angleBetweenVectors(axesN, osg::Vec3f(1, 0, 0));
float phiX = angleBetweenVectors(axisN, osg::Vec3f(1, 0, 0));
aux = rotateX(aux, phiX);
//angle between vec and x, rotate in z axes
float phiY = angleBetweenVectors(axesN, osg::Vec3f(0, 1, 0));
//angle between vec and x, rotate in z axis
float phiY = angleBetweenVectors(axisN, osg::Vec3f(0, 1, 0));
aux = rotateY(aux, phiY);
// rotate around z
aux = rotateZ(aux, phi);
Expand All @@ -221,30 +281,44 @@ osg::Vec3f Spring::rotateArbitraryAxes_expensive(osg::Vec3f vec, osg::Vec3f axes
return aux;
}

osg::Vec3f Spring::rotateArbitraryAxes(osg::Vec3f vec, osg::Vec3f axes, float phi)
/*!
* \brief Spring::rotateArbitraryAxis
* \param vector1
* \param rotation axis
* \param angle
* rotates the vector around the given axis with angle phi
* \return the rotated vector
*/
osg::Vec3f Spring::rotateArbitraryAxis(osg::Vec3f vec, osg::Vec3f axis, float phi)
{
osg::Vec3f axesN = normalize(axes);
float M1_1 = (1 - std::cos(phi)) * axesN[0] * axesN[0] + std::cos(phi) * 1 + std::sin(phi) * 0;
float M1_2 = (1 - std::cos(phi)) * axesN[0] * axesN[1] + std::cos(phi) * 0 + std::sin(phi) * (-axesN[2]);
float M1_3 = (1 - std::cos(phi)) * axesN[0] * axesN[2] + std::cos(phi) * 0 + std::sin(phi) * (axesN[1]);
float M2_1 = (1 - std::cos(phi)) * axesN[0] * axesN[1] + std::cos(phi) * 0 + std::sin(phi) * (axesN[2]);
float M2_2 = (1 - std::cos(phi)) * axesN[1] * axesN[1] + std::cos(phi) * 1 + std::sin(phi) * 0;
float M2_3 = (1 - std::cos(phi)) * axesN[1] * axesN[2] + std::cos(phi) * 0 + std::sin(phi) * (-axesN[0]);
float M3_1 = (1 - std::cos(phi)) * axesN[0] * axesN[2] + std::cos(phi) * 0 + std::sin(phi) * (-axesN[1]);
float M3_2 = (1 - std::cos(phi)) * axesN[1] * axesN[2] + std::cos(phi) * 0 + std::sin(phi) * (axesN[0]);
float M3_3 = (1 - std::cos(phi)) * axesN[2] * axesN[2] + std::cos(phi) * 1 + std::sin(phi) * 0;
osg::Vec3f axisN = normalize(axis);
float M1_1 = (1 - std::cos(phi)) * axisN[0] * axisN[0] + std::cos(phi) * 1 + std::sin(phi) * 0;
float M1_2 = (1 - std::cos(phi)) * axisN[0] * axisN[1] + std::cos(phi) * 0 + std::sin(phi) * (-axisN[2]);
float M1_3 = (1 - std::cos(phi)) * axisN[0] * axisN[2] + std::cos(phi) * 0 + std::sin(phi) * (axisN[1]);
float M2_1 = (1 - std::cos(phi)) * axisN[0] * axisN[1] + std::cos(phi) * 0 + std::sin(phi) * (axisN[2]);
float M2_2 = (1 - std::cos(phi)) * axisN[1] * axisN[1] + std::cos(phi) * 1 + std::sin(phi) * 0;
float M2_3 = (1 - std::cos(phi)) * axisN[1] * axisN[2] + std::cos(phi) * 0 + std::sin(phi) * (-axisN[0]);
float M3_1 = (1 - std::cos(phi)) * axisN[0] * axisN[2] + std::cos(phi) * 0 + std::sin(phi) * (-axisN[1]);
float M3_2 = (1 - std::cos(phi)) * axisN[1] * axisN[2] + std::cos(phi) * 0 + std::sin(phi) * (axisN[0]);
float M3_3 = (1 - std::cos(phi)) * axisN[2] * axisN[2] + std::cos(phi) * 1 + std::sin(phi) * 0;
return osg::Vec3f(M1_1*vec[0]+ M1_2*vec[1]+ M1_3*vec[2], M2_1*vec[0] + M2_2*vec[1] + M2_3*vec[2], M3_1*vec[0] + M3_2*vec[1] + M3_3*vec[2]);
}




Spring::Spring(float r, float rCoil, float nWindings, float l) :
/*!
* \brief Spring::Spring
* \param center radius of the coil
* \param radius of the wire
* \param number of windings
* \param the length
* creates an osg spring geometry
*/
Spring::Spring(float r, float rWire, float nWindings, float l) :
osg::Geometry()
{
float R = r;
float L = l;
float RCOIL = rCoil;
float RWIRE = rWire;
float NWIND = nWindings;

const int ELEMENTS_WINDING = 10;
Expand All @@ -254,41 +328,41 @@ Spring::Spring(float r, float rCoil, float nWindings, float l) :

//the inner line points
int numSegments = (ELEMENTS_WINDING * NWIND) + 1;
splineVertices = new osg::Vec3Array(numSegments);
mpSplineVertices = new osg::Vec3Array(numSegments);

for (int segIdx = 0; segIdx < numSegments; segIdx++)
{
float x = std::sin(2 * M_PI / ELEMENTS_WINDING * segIdx) * R;
float y = std::cos(2 * M_PI / ELEMENTS_WINDING * segIdx) * R;
float z = L / numSegments * segIdx;
(*splineVertices)[segIdx].set(osg::Vec3(x,y,z));
(*mpSplineVertices)[segIdx].set(osg::Vec3(x,y,z));
}

//the outer points for the facettes
int numVertices = (numSegments + 1)*ELEMENTS_CONTOUR;
outerVertices = new osg::Vec3Array(numVertices);
mpOuterVertices = new osg::Vec3Array(numVertices);
osg::Vec3f normal;
osg::Vec3f v1;
osg::Vec3f v2;
int vertIdx = 0;
for (int i = 0; i < numSegments-1; i++)
{
v1 = splineVertices->at(i);
v2 = splineVertices->at(i + 1);
v1 = mpSplineVertices->at(i);
v2 = mpSplineVertices->at(i + 1);
normal = osg::Vec3f(v2[0] - v1[0], v2[1] - v1[1], v2[2] - v1[2]);
osg::Vec3f vec0 = normal;
normal = getNormal(normal, RCOIL);
normal = getNormal(normal, RWIRE);
for (int i1 = 0; i1 < ELEMENTS_CONTOUR; i1++)
{
float angle = M_PI * 2 / ELEMENTS_CONTOUR * i1;
osg::Vec3f a1 = rotateArbitraryAxes(normal, vec0, angle);
(*outerVertices)[vertIdx].set(osg::Vec3f((v1[0] + a1[0]), (v1[1] + a1[1]), (v1[2] + a1[2])));
osg::Vec3f a1 = rotateArbitraryAxis(normal, vec0, angle);
(*mpOuterVertices)[vertIdx].set(osg::Vec3f((v1[0] + a1[0]), (v1[1] + a1[1]), (v1[2] + a1[2])));
vertIdx++;
}
}

// pass the created vertex array to the points geometry object.
this->setVertexArray(outerVertices);
this->setVertexArray(mpOuterVertices);

//PLANES
// base plane bottom
Expand All @@ -302,7 +376,7 @@ Spring::Spring(float r, float rCoil, float nWindings, float l) :
basePlane->push_back(i + ELEMENTS_CONTOUR-1);
this->addPrimitiveSet(basePlane);
}
//std::cout << "NUM " << outerVertices->size() << std::endl;
//this->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, outerVertices->size()));
//std::cout << "NUM " << mpOuterVertices->size() << std::endl;
//this->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, mpOuterVertices->size()));
}

8 changes: 4 additions & 4 deletions OMEdit/OMEditGUI/Animation/ExtraShapes.h
Expand Up @@ -60,13 +60,13 @@ class Spring : public osg::Geometry
osg::Vec3f rotateX(osg::Vec3f vec, float phi);
osg::Vec3f rotateY(osg::Vec3f vec, float phi);
osg::Vec3f rotateZ(osg::Vec3f vec, float phi);
osg::Vec3f rotateArbitraryAxes_expensive(osg::Vec3f vec, osg::Vec3f axes, float phi);
osg::Vec3f rotateArbitraryAxes(osg::Vec3f vec, osg::Vec3f axes, float phi);
osg::Vec3f rotateArbitraryAxis_expensive(osg::Vec3f vec, osg::Vec3f axis, float phi);
osg::Vec3f rotateArbitraryAxis(osg::Vec3f vec, osg::Vec3f axis, float phi);
float absoluteVector(osg::Vec3f vec);
float angleBetweenVectors(osg::Vec3f vec1, osg::Vec3f vec2);

osg::Vec3Array* outerVertices;
osg::Vec3Array* splineVertices;
osg::Vec3Array* mpOuterVertices;
osg::Vec3Array* mpSplineVertices;
};

#endif //end EXTRASHAPES_H

0 comments on commit f03fd1f

Please sign in to comment.