Skip to content

Commit

Permalink
Merge pull request #9 from Supporting/bugfix-sloped-cylinder
Browse files Browse the repository at this point in the history
Bugfix sloped cylinder
  • Loading branch information
chmouton committed Jan 30, 2016
2 parents 81ca015 + 059c2c1 commit fa139c8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 45 deletions.
69 changes: 38 additions & 31 deletions src/api/rvmmeshhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <algorithm>
#include <cmath>

#include <Eigen/Geometry>

#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
Expand Down Expand Up @@ -128,7 +130,7 @@ const Mesh RVMMeshHelper2::makeBox(const Primitives::Box& box, const float &maxS

const Mesh RVMMeshHelper2::makeSphere(const Primitives::Sphere &sphere, const float& maxSideSize, const int& minSides)
{
const float radius = sphere.diamater / 2.0f;
const float radius = sphere.diameter / 2.0f;

// Init sphere
int sides = max(8, minSides);
Expand Down Expand Up @@ -586,43 +588,49 @@ unsigned long RVMMeshHelper2::infoSnoutNumSides(const Primitives::Snout &snout,

const Mesh RVMMeshHelper2::makeSnout(const Primitives::Snout& snout, unsigned long sides)
{
const float rbottom = snout.dbottom();
const float rtop = snout.dtop();
const float rbottom = snout.dbottom();
const float height = snout.height();
const float xoffset = snout.xoffset();
const float yoffset = snout.yoffset();
const float heightXOffsetTop = rtop * tan(snout.xtshear());
const float heightYOffsetTop = rtop * tan(snout.ytshear());
const float heightXOffsetBottom = rbottom * tan(snout.xbshear());
const float heightYOffsetBottom = rbottom * tan(snout.ybshear());

vector<unsigned long> index;
vector<Vector3F> points;
vector<Vector3F> normals;
vector<unsigned long> index;
vector<unsigned long> normalindex;
vector<Vector3F> vectors;

const float hh = height / 2;
const float halfHeight = height / 2;

// Vector3Fes and normals
Vector3F v;
Vector3F n;
const float da = 2.0f * M_PI / static_cast<float>(sides);

const float da = float(2.0f * M_PI / static_cast<float>(sides));
for (unsigned long i = 0; i < sides; i++)
{
const float a = static_cast<float>(i)* da;
const float c = cos(a);
const float s = sin(a);
const float x = sin(a);
const float y = cos(a);

v[0] = rbottom * c; v[1] = rbottom * s; v[2] = -hh;
v[0] = rbottom * x;
v[1] = rbottom * y;
v[2] = -halfHeight + heightXOffsetBottom * x + heightYOffsetBottom * y;
points.push_back(v);
v[0] = rtop * c + xoffset; v[1] = rtop * s + yoffset; v[2] = hh;

v[0] = rtop * x + xoffset;
v[1] = rtop * y + yoffset;
v[2] = halfHeight + heightXOffsetTop * x + heightYOffsetTop * y;
points.push_back(v);
if (height > 0.0f)
{
float dh = sqrt(((rtop * c + xoffset - rbottom * c)*(rtop * c + xoffset - rbottom * c) + (rtop * s + yoffset - rbottom * s)*(rtop * s + yoffset - rbottom * s)) / (height*height));
n[0] = c; n[1] = s; n[2] = (rtop < rbottom) ? dh : -dh;
if (height > 0.0f) {
normals.push_back(Vector3F(x,y,0));
}
else {
n[0] = 0; n[1] = 0; n[2] = 1;
normals.push_back(Vector3F(0,0,1));
}
n.normalize();
vectors.push_back(n);
}

// Sides
Expand All @@ -646,19 +654,17 @@ const Mesh RVMMeshHelper2::makeSnout(const Primitives::Snout& snout, unsigned lo
}

// Caps
// - Caps normals
const unsigned long nci = static_cast<unsigned long>(vectors.size());
n[0] = 0; n[1] = 0; n[2] = -1;
vectors.push_back(n);
n[0] = 0; n[1] = 0; n[2] = 1;
vectors.push_back(n);
// - Caps centers
// - Cap normals
const unsigned long nci = static_cast<unsigned long>(normals.size());
normals.push_back(Vector3F(sin(snout.xbshear())*cos(snout.ybshear()),sin(snout.ybshear()),-cos(snout.xbshear())*cos(snout.ybshear())));
normals.push_back(Vector3F(-sin(snout.xtshear())*cos(snout.ytshear()),-sin(snout.ytshear()),cos(snout.xtshear())*cos(snout.ytshear())));

// - Cap centers
const unsigned long ci = static_cast<unsigned long>(points.size());
v[0] = 0; v[1] = 0; v[2] = -hh;
points.push_back(v);
v[0] = xoffset; v[1] = yoffset; v[2] = hh;
points.push_back(v);
// - Caps indexes
points.push_back(Vector3F(0 ,0, -halfHeight));
points.push_back(Vector3F(0, 0, halfHeight));

// - Bottom caps indexes
for (unsigned long j = 0; j < sides; j++)
{
index.push_back(j * 2);
Expand All @@ -669,6 +675,7 @@ const Mesh RVMMeshHelper2::makeSnout(const Primitives::Snout& snout, unsigned lo
normalindex.push_back(nci);
}

// - Top caps indexes
for (unsigned long j = 0; j < sides; j++)
{
index.push_back(j * 2 + 1);
Expand All @@ -682,7 +689,7 @@ const Mesh RVMMeshHelper2::makeSnout(const Primitives::Snout& snout, unsigned lo
Mesh result;
result.positions = points;
result.positionIndex = index;
result.normals = vectors;
result.normals = normals;
result.normalIndex = normalindex;
return result;
}
Expand Down Expand Up @@ -774,7 +781,7 @@ const Mesh RVMMeshHelper2::makeSphericalDish(const Primitives::SphericalDish& sD
if (sDish.height() >= dishradius * 2)
{
Primitives::Sphere s;
s.diamater = dishradius * 2;
s.diameter = dishradius * 2;

return makeSphere(s, maxSideSize, minSides);
}
Expand Down
1 change: 0 additions & 1 deletion src/api/rvmparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ bool RVMParser::readPrimitive(std::istream& is)
case 7:
m_nbSnouts++;
readArray_(is, primitive.snout.data);
skip_<4>(is);

m_reader.createSnout(matrix, primitive.snout);
break;
Expand Down
13 changes: 11 additions & 2 deletions src/api/rvmprimitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ namespace Primitives
inline float& yoffset() { return data[4]; }
inline const float& yoffset() const { return data[4]; }

float data[5];
inline float& xbshear() { return data[5]; }
inline const float& xbshear() const { return data[5]; }
inline float& ybshear() { return data[6]; }
inline const float& ybshear() const { return data[6]; }
inline float& xtshear() { return data[7]; }
inline const float& xtshear() const { return data[7]; }
inline float& ytshear() { return data[8]; }
inline const float& ytshear() const { return data[8]; }

float data[9];
};

struct Cylinder
Expand All @@ -105,7 +114,7 @@ namespace Primitives

struct Sphere
{
float diamater;
float diameter;
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/converters/colladaconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ void COLLADAConverter::createSnout(const std::array<float, 12>& matrix, const Pr
params.push_back(snout.height());
params.push_back(snout.xoffset());
params.push_back(snout.yoffset());
params.push_back(snout.xbshear());
params.push_back(snout.ybshear());
params.push_back(snout.xtshear());
params.push_back(snout.ytshear());

string gid = getInstanceName(params);
if(gid.empty()) {
Expand Down Expand Up @@ -520,7 +524,7 @@ void COLLADAConverter::createCylinder(const std::array<float, 12>& matrix, const
void COLLADAConverter::createSphere(const std::array<float, 12>& matrix, const Primitives::Sphere& sphere) {
std::vector<float> params;
params.push_back(Sphere);
params.push_back(sphere.diamater);
params.push_back(sphere.diameter);

string gid = getInstanceName(params);
if(gid.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/converters/dslconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void DSLConverter::createCylinder(const std::array<float, 12>& matrix, const Pri

void DSLConverter::createSphere(const std::array<float, 12>& matrix, const Primitives::Sphere& params) {
string shapeid = static_cast<ostringstream*>( &(ostringstream() << m_lastShapeId++) )->str();
writer->writeSphere("bshape" + shapeid, params.diamater/2);
writer->writeSphere("bshape" + shapeid, params.diameter/2);
writeShapeTransforms(shapeid, matrix);
}

Expand Down
8 changes: 6 additions & 2 deletions src/converters/x3dconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ void X3DConverter::createSnout(const std::array<float, 12>& matrix, const Primit
params.push_back(snout.height());
params.push_back(snout.xoffset());
params.push_back(snout.yoffset());
params.push_back(snout.xbshear());
params.push_back(snout.ybshear());
params.push_back(snout.xtshear());
params.push_back(snout.ytshear());

pair<string,int> gid = getInstanceName(params);
if(gid.first.empty()) {
Expand Down Expand Up @@ -398,12 +402,12 @@ void X3DConverter::createSphere(const std::array<float, 12>& matrix, const Primi
startShape(matrix);
if (m_primitives) {
startNode(ID::Sphere);
m_writers.back()->setSFFloat(ID::radius, sphere.diamater);
m_writers.back()->setSFFloat(ID::radius, sphere.diameter);
endNode(ID::Sphere);
} else {
std::vector<float> params;
params.push_back(Sphere);
params.push_back(sphere.diamater);
params.push_back(sphere.diameter);

pair<string,int> gid = getInstanceName(params);
if(gid.first.empty()) {
Expand Down
18 changes: 11 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void printStats(time_t duration, RVMParser &parser) {

int main(int argc, char** argv)
{
cout << "Plant Mock-Up Converter 1.0.0\nCopyright (C) EDF 2016" << endl;
cout << "Plant Mock-Up Converter 1.0.1\nCopyright (C) EDF 2016" << endl;

argc -= (argc > 0); argv += (argc > 0);
option::Stats stats(usage, argc, argv);
Expand Down Expand Up @@ -255,11 +255,15 @@ int main(int argc, char** argv)
} break;
case SNOUT: {
Primitives::Snout snout;
snout.data[0] = 2.0;
snout.data[1] = 4.0;
snout.data[2] = 2.0;
snout.data[3] = 1.0;
snout.data[4] = 1.0;
snout.data[0] = 2.0f; // bottom radius
snout.data[1] = 2.0f; // top radius
snout.data[2] = 5.0f; // height
snout.data[3] = 0.0f; // xoffset
snout.data[4] = 0.0f; // yoffset
snout.data[5] = 0.0f; // xbottomNormalOffset
snout.data[6] = 0.4f; // ybottomNormalOffset
snout.data[7] = 0.0f; // xtopNormalOffset
snout.data[8] = -0.4f; // ytopNormalOffset

reader->createSnout(matrix, snout);
} break;
Expand All @@ -271,7 +275,7 @@ int main(int argc, char** argv)
} break;
case SPHERE: {
Primitives::Sphere sphere;
sphere.diamater = 2.0;
sphere.diameter = 2.0;
reader->createSphere(matrix, sphere);
} break;
case CIRCULARTORUS: {
Expand Down

0 comments on commit fa139c8

Please sign in to comment.