Clean up boudnary code
This commit is contained in:
@@ -32,99 +32,6 @@ extern int main_orig(int, char**);
|
||||
extern int main_test(int, char**);
|
||||
|
||||
|
||||
osg::Vec3 computeRayIntersectionPoint(const osg::Vec3& a, const osg::Vec3& an, const osg::Vec3& c, const osg::Vec3& cn)
|
||||
{
|
||||
float denominator = ( cn.x() * an.y() - cn.y() * an.x());
|
||||
if (denominator==0.0)
|
||||
{
|
||||
//OSG_NOTICE<<"computeRayIntersectionPoint()<<denominator==0.0"<<std::endl;
|
||||
// line segments must be parallel.
|
||||
return (a+c)*0.5;
|
||||
}
|
||||
|
||||
float t = ((a.x()-c.x())*an.y() - (a.y()-c.y())*an.x()) / denominator;
|
||||
return c + cn*t;
|
||||
}
|
||||
|
||||
osg::Vec3 computeIntersectionPoint(const osg::Vec3& a, const osg::Vec3& b, const osg::Vec3& c, const osg::Vec3& d)
|
||||
{
|
||||
#if 0
|
||||
float ba_x = b.x()-a.x();
|
||||
float ba_y = b.y()-a.y();
|
||||
|
||||
float dc_x = d.x()-c.x();
|
||||
float dc_y = d.y()-c.y();
|
||||
|
||||
float denominator = (dc_x * ba_y - dc_y * ba_x);
|
||||
if (denominator==0.0)
|
||||
{
|
||||
// line segments must be parallel.
|
||||
return (b+c)*0.5;
|
||||
}
|
||||
|
||||
float t = ((a.x()-c.x())*ba_y + (a.y()-c.y())*ba_x) / denominator;
|
||||
|
||||
return c + (d-c)*t;
|
||||
#endif
|
||||
return computeRayIntersectionPoint(a, b-a, c, d-c);
|
||||
|
||||
}
|
||||
|
||||
osg::Vec3 computeBisectorNormal(const osg::Vec3& a, const osg::Vec3& b, const osg::Vec3& c, const osg::Vec3& d)
|
||||
{
|
||||
osg::Vec2 ab(a.x()-b.x(), a.y()-b.y());
|
||||
osg::Vec2 dc(d.x()-c.x(), d.y()-c.y());
|
||||
/*float length_ab =*/ ab.normalize();
|
||||
/*float length_dc =*/ dc.normalize();
|
||||
|
||||
float e = dc.y() - ab.y();
|
||||
float f = ab.x() - dc.x();
|
||||
float denominator = sqrtf(e*e + f*f);
|
||||
float nx = e / denominator;
|
||||
float ny = f / denominator;
|
||||
if (( ab.x()*ny - ab.y()*nx) > 0.0f)
|
||||
{
|
||||
// OSG_NOTICE<<" computeBisectorNormal(a=["<<a<<"], b=["<<b<<"], c=["<<c<<"], d=["<<d<<"]), nx="<<nx<<", ny="<<ny<<", denominator="<<denominator<<" no need to swap"<<std::endl;
|
||||
return osg::Vec3(nx,ny,0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_NOTICE<<" computeBisectorNormal(a=["<<a<<"], b=["<<b<<"], c=["<<c<<"], d=["<<d<<"]), nx="<<nx<<", ny="<<ny<<", denominator="<<denominator<<" need to swap!!!"<<std::endl;
|
||||
return osg::Vec3(-nx,-ny,0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
float computeBisectorIntersectorThickness(const osg::Vec3& a, const osg::Vec3& b, const osg::Vec3& c, const osg::Vec3& d, const osg::Vec3& e, const osg::Vec3& f)
|
||||
{
|
||||
osg::Vec3 intersection_abcd = computeIntersectionPoint(a,b,c,d);
|
||||
osg::Vec3 bisector_abcd = computeBisectorNormal(a,b,c,d);
|
||||
osg::Vec3 intersection_cdef = computeIntersectionPoint(c,d,e,f);
|
||||
osg::Vec3 bisector_cdef = computeBisectorNormal(c,d,e,f);
|
||||
if (bisector_abcd==bisector_cdef)
|
||||
{
|
||||
//OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl;
|
||||
//OSG_NOTICE<<" bisectors parallel, thickness = "<<FLT_MAX<<std::endl;
|
||||
return FLT_MAX;
|
||||
}
|
||||
|
||||
osg::Vec3 bisector_intersection = computeRayIntersectionPoint(intersection_abcd,bisector_abcd, intersection_cdef, bisector_cdef);
|
||||
osg::Vec3 normal(d.y()-c.y(), c.x()-d.x(), 0.0);
|
||||
float cd_length = normal.normalize();
|
||||
if (cd_length==0)
|
||||
{
|
||||
//OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl;
|
||||
//OSG_NOTICE<<" segment length==0, thickness = "<<FLT_MAX<<std::endl;
|
||||
return FLT_MAX;
|
||||
}
|
||||
|
||||
float thickness = (bisector_intersection - c) * normal;
|
||||
#if 0
|
||||
OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl;
|
||||
OSG_NOTICE<<" bisector_abcd = "<<bisector_abcd<<", bisector_cdef="<<bisector_cdef<<std::endl;
|
||||
OSG_NOTICE<<" bisector_intersection = "<<bisector_intersection<<", thickness = "<<thickness<<std::endl;
|
||||
#endif
|
||||
return thickness;
|
||||
}
|
||||
|
||||
class Boundary
|
||||
{
|
||||
@@ -156,6 +63,81 @@ public:
|
||||
|
||||
}
|
||||
|
||||
osg::Vec3 computeRayIntersectionPoint(const osg::Vec3& a, const osg::Vec3& an, const osg::Vec3& c, const osg::Vec3& cn)
|
||||
{
|
||||
float denominator = ( cn.x() * an.y() - cn.y() * an.x());
|
||||
if (denominator==0.0)
|
||||
{
|
||||
//OSG_NOTICE<<"computeRayIntersectionPoint()<<denominator==0.0"<<std::endl;
|
||||
// line segments must be parallel.
|
||||
return (a+c)*0.5;
|
||||
}
|
||||
|
||||
float t = ((a.x()-c.x())*an.y() - (a.y()-c.y())*an.x()) / denominator;
|
||||
return c + cn*t;
|
||||
}
|
||||
|
||||
osg::Vec3 computeIntersectionPoint(const osg::Vec3& a, const osg::Vec3& b, const osg::Vec3& c, const osg::Vec3& d)
|
||||
{
|
||||
return computeRayIntersectionPoint(a, b-a, c, d-c);
|
||||
}
|
||||
|
||||
osg::Vec3 computeBisectorNormal(const osg::Vec3& a, const osg::Vec3& b, const osg::Vec3& c, const osg::Vec3& d)
|
||||
{
|
||||
osg::Vec2 ab(a.x()-b.x(), a.y()-b.y());
|
||||
osg::Vec2 dc(d.x()-c.x(), d.y()-c.y());
|
||||
/*float length_ab =*/ ab.normalize();
|
||||
/*float length_dc =*/ dc.normalize();
|
||||
|
||||
float e = dc.y() - ab.y();
|
||||
float f = ab.x() - dc.x();
|
||||
float denominator = sqrtf(e*e + f*f);
|
||||
float nx = e / denominator;
|
||||
float ny = f / denominator;
|
||||
if (( ab.x()*ny - ab.y()*nx) > 0.0f)
|
||||
{
|
||||
// OSG_NOTICE<<" computeBisectorNormal(a=["<<a<<"], b=["<<b<<"], c=["<<c<<"], d=["<<d<<"]), nx="<<nx<<", ny="<<ny<<", denominator="<<denominator<<" no need to swap"<<std::endl;
|
||||
return osg::Vec3(nx,ny,0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_NOTICE<<" computeBisectorNormal(a=["<<a<<"], b=["<<b<<"], c=["<<c<<"], d=["<<d<<"]), nx="<<nx<<", ny="<<ny<<", denominator="<<denominator<<" need to swap!!!"<<std::endl;
|
||||
return osg::Vec3(-nx,-ny,0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
float computeBisectorIntersectorThickness(const osg::Vec3& a, const osg::Vec3& b, const osg::Vec3& c, const osg::Vec3& d, const osg::Vec3& e, const osg::Vec3& f)
|
||||
{
|
||||
osg::Vec3 intersection_abcd = computeIntersectionPoint(a,b,c,d);
|
||||
osg::Vec3 bisector_abcd = computeBisectorNormal(a,b,c,d);
|
||||
osg::Vec3 intersection_cdef = computeIntersectionPoint(c,d,e,f);
|
||||
osg::Vec3 bisector_cdef = computeBisectorNormal(c,d,e,f);
|
||||
if (bisector_abcd==bisector_cdef)
|
||||
{
|
||||
//OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl;
|
||||
//OSG_NOTICE<<" bisectors parallel, thickness = "<<FLT_MAX<<std::endl;
|
||||
return FLT_MAX;
|
||||
}
|
||||
|
||||
osg::Vec3 bisector_intersection = computeRayIntersectionPoint(intersection_abcd,bisector_abcd, intersection_cdef, bisector_cdef);
|
||||
osg::Vec3 normal(d.y()-c.y(), c.x()-d.x(), 0.0);
|
||||
float cd_length = normal.normalize();
|
||||
if (cd_length==0)
|
||||
{
|
||||
//OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl;
|
||||
//OSG_NOTICE<<" segment length==0, thickness = "<<FLT_MAX<<std::endl;
|
||||
return FLT_MAX;
|
||||
}
|
||||
|
||||
float thickness = (bisector_intersection - c) * normal;
|
||||
#if 0
|
||||
OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl;
|
||||
OSG_NOTICE<<" bisector_abcd = "<<bisector_abcd<<", bisector_cdef="<<bisector_cdef<<std::endl;
|
||||
OSG_NOTICE<<" bisector_intersection = "<<bisector_intersection<<", thickness = "<<thickness<<std::endl;
|
||||
#endif
|
||||
return thickness;
|
||||
}
|
||||
|
||||
float computeThickness(unsigned int i)
|
||||
{
|
||||
Segment& seg_before = _segments[ (i+_segments.size()-1) % _segments.size() ];
|
||||
@@ -265,203 +247,6 @@ public:
|
||||
};
|
||||
|
||||
|
||||
float computeAngle(osg::Vec3& v1, osg::Vec3& v2, osg::Vec3& v3)
|
||||
{
|
||||
osg::Vec3 v12(v1-v2);
|
||||
osg::Vec3 v32(v3-v2);
|
||||
v12.normalize();
|
||||
v32.normalize();
|
||||
float dot = v12*v32;
|
||||
float angle = acosf(dot);
|
||||
return angle;
|
||||
}
|
||||
|
||||
void computeBoundaryAngles(osg::Vec3Array& vertices, unsigned int start, unsigned int count)
|
||||
{
|
||||
//OSG_NOTICE<<"computeBoundaryAngles("<<vertices.size()<<", "<<start<<", "<<count<<")"<<std::endl;
|
||||
if (vertices[start+count-1]==vertices[start])
|
||||
{
|
||||
// OSG_NOTICE<<"is a line loop"<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_NOTICE<<"is not line loop, ("<<vertices[start+count-1]<<"), ("<<vertices[start]<<")"<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
computeAngle(vertices[start+count-2],vertices[start],vertices[start+1]);
|
||||
for(unsigned int i=start+1; i<start+count-1; ++i)
|
||||
{
|
||||
computeAngle(vertices[i-1],vertices[i],vertices[i+1]);
|
||||
}
|
||||
computeAngle(vertices[start+count-2],vertices[start],vertices[start+1]);
|
||||
}
|
||||
|
||||
void computeBoundaryAngles(osg::Geometry* geometry)
|
||||
{
|
||||
OSG_NOTICE<<"computeBoundaryAngles("<<geometry<<")"<<std::endl;
|
||||
osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
|
||||
osg::Geometry::PrimitiveSetList& primitives = geometry->getPrimitiveSetList();
|
||||
for(osg::Geometry::PrimitiveSetList::iterator itr = primitives.begin();
|
||||
itr != primitives.end();
|
||||
++itr)
|
||||
{
|
||||
osg::DrawArrays* drawArray = dynamic_cast<osg::DrawArrays*>(itr->get());
|
||||
if (drawArray && drawArray->getMode()==GL_POLYGON)
|
||||
{
|
||||
computeBoundaryAngles(*vertices, drawArray->getFirst(), drawArray->getCount());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
osg::Vec3 computeNewVertexPosition(osg::Vec3& v1, osg::Vec3& v2, osg::Vec3& v3)
|
||||
{
|
||||
double angle = computeAngle(v1,v2,v3);
|
||||
osg::Vec3 v21(v2-v1);
|
||||
osg::Vec3 v32(v3-v2);
|
||||
float length_21 = v21.normalize();
|
||||
float length_32 = v32.normalize();
|
||||
|
||||
float t = 5.0;
|
||||
if (length_21==0.0)
|
||||
{
|
||||
OSG_NOTICE<<"length_21=="<<length_21<<", length_32="<<length_32<<std::endl;
|
||||
osg::Vec3 bisector = v32 ^ osg::Vec3(0.0f,0.0f,1.0f);
|
||||
bisector.normalize();
|
||||
osg::Vec3 new_vertex = v2 + bisector * t;
|
||||
return new_vertex;
|
||||
}
|
||||
else if (length_32==0.0)
|
||||
{
|
||||
OSG_NOTICE<<"length_21=="<<length_21<<", length_32="<<length_32<<std::endl;
|
||||
osg::Vec3 bisector = v21 ^ osg::Vec3(0.0f,0.0f,1.0f);
|
||||
bisector.normalize();
|
||||
osg::Vec3 new_vertex = v2 + bisector * t;
|
||||
return new_vertex;
|
||||
}
|
||||
|
||||
osg::Vec3 cross = v21^v32;
|
||||
osg::Vec3 bisector(v32-v21);
|
||||
|
||||
#if 0
|
||||
OSG_NOTICE<<"v1=["<<v1<<"], v2=["<<v2<<"], v3=["<<v3<<"], dot_angle="<<osg::RadiansToDegrees(angle)<<std::endl;
|
||||
OSG_NOTICE<<" computeIntersectionPoint() point "<<computeIntersectionPoint(v1,v2,v2,v3)<<std::endl;
|
||||
#endif
|
||||
// OSG_NOTICE<<" computeBisectorNormal() normal "<<computeBisectorNormal(v1,v2,v2,v3)<<std::endl;
|
||||
|
||||
if (bisector.length()<0.5)
|
||||
{
|
||||
// angle wider than 90 degrees so use side vectors as guide for angle to project along.
|
||||
osg::Vec3 s21 = v21 ^ osg::Vec3(0.0f,0.0f,1.0f);
|
||||
s21.normalize();
|
||||
|
||||
osg::Vec3 s32 = v32 ^ osg::Vec3(0.0f,0.0f,1.0f);
|
||||
s32.normalize();
|
||||
|
||||
osg::Vec3 bisector(s21+s32);
|
||||
bisector.normalize();
|
||||
|
||||
|
||||
if ((computeBisectorNormal(v1,v2,v2,v3)-bisector).length2()>0.001)
|
||||
{
|
||||
OSG_NOTICE<<" WARNING 1 bisector disagree "<<bisector<<", s21=["<<s21<<"], s32=["<<s32<<"]"<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if 0
|
||||
OSG_NOTICE<<" bisector normal "<<bisector<<std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
float l = t / sin(angle*0.5);
|
||||
|
||||
osg::Vec3 new_vertex = v2 + bisector * l;
|
||||
new_vertex.z() += 0.5f;
|
||||
|
||||
return new_vertex;
|
||||
}
|
||||
else
|
||||
{
|
||||
float l = t / sin(angle*0.5);
|
||||
|
||||
bisector.normalize();
|
||||
if (cross.z()>0.0) bisector = -bisector;
|
||||
|
||||
if ((computeBisectorNormal(v1,v2,v2,v3)-bisector).length2()>0.001)
|
||||
{
|
||||
OSG_NOTICE<<" WARNING 2 bisector disagree "<<bisector<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if 0
|
||||
OSG_NOTICE<<" bisector normal "<<bisector<<std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
osg::Vec3 new_vertex = v2 + bisector * l;
|
||||
new_vertex.z() += 0.5f;
|
||||
return new_vertex;
|
||||
}
|
||||
}
|
||||
|
||||
osg::DrawArrays* computeBevelEdge(osg::Vec3Array& orig_vertices, unsigned int start, unsigned int count, osg::Vec3Array& new_vertices)
|
||||
{
|
||||
|
||||
// OSG_NOTICE<<"computeBevelEdge("<<orig_vertices.size()<<", "<<start<<", "<<count<<")"<<std::endl;
|
||||
if (orig_vertices[start+count-1]==orig_vertices[start])
|
||||
{
|
||||
// OSG_NOTICE<<"is a line loop"<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_NOTICE<<"is not line loop, ("<<orig_vertices[start+count-1]<<"), ("<<orig_vertices[start]<<")"<<std::endl;
|
||||
return new osg::DrawArrays(GL_POLYGON, start, count);
|
||||
}
|
||||
|
||||
new_vertices[start] = computeNewVertexPosition(orig_vertices[start+count-2],orig_vertices[start],orig_vertices[start+1]);
|
||||
for(unsigned int i=start+1; i<start+count-1; ++i)
|
||||
{
|
||||
new_vertices[i] = computeNewVertexPosition(orig_vertices[i-1],orig_vertices[i],orig_vertices[i+1]);
|
||||
}
|
||||
new_vertices[start+count-1] = computeNewVertexPosition(orig_vertices[start+count-2],orig_vertices[start],orig_vertices[start+1]);
|
||||
|
||||
return new osg::DrawArrays(GL_POLYGON, start, count);
|
||||
}
|
||||
|
||||
void removeLoops(osg::Vec3Array& orig_vertices, unsigned int start, unsigned int count)
|
||||
{
|
||||
}
|
||||
|
||||
osg::Geometry* computeBevelEdge(osg::Geometry* orig_geometry)
|
||||
{
|
||||
// OSG_NOTICE<<"computeBoundaryAngles("<<orig_geometry<<")"<<std::endl;
|
||||
osg::Vec3Array* orig_vertices = dynamic_cast<osg::Vec3Array*>(orig_geometry->getVertexArray());
|
||||
osg::Geometry::PrimitiveSetList& orig_primitives = orig_geometry->getPrimitiveSetList();
|
||||
|
||||
osg::Geometry* new_geometry = new osg::Geometry;
|
||||
osg::Vec3Array* new_vertices = new osg::Vec3Array(*orig_vertices);
|
||||
osg::Geometry::PrimitiveSetList& new_primitives = new_geometry->getPrimitiveSetList();
|
||||
new_geometry->setVertexArray(new_vertices);
|
||||
osg::Vec4Array* new_colours = new osg::Vec4Array;
|
||||
new_colours->push_back(osg::Vec4(1.0,0.0,0.0,1.0));
|
||||
new_geometry->setColorArray(new_colours);
|
||||
new_geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
|
||||
|
||||
for(osg::Geometry::PrimitiveSetList::iterator itr = orig_primitives.begin();
|
||||
itr != orig_primitives.end();
|
||||
++itr)
|
||||
{
|
||||
osg::DrawArrays* drawArray = dynamic_cast<osg::DrawArrays*>(itr->get());
|
||||
if (drawArray && drawArray->getMode()==GL_POLYGON)
|
||||
{
|
||||
osg::DrawArrays* new_drawArray = computeBevelEdge(*orig_vertices, drawArray->getFirst(), drawArray->getCount(), *new_vertices);
|
||||
removeLoops(*new_vertices, new_drawArray->getFirst(), new_drawArray->getCount());
|
||||
new_primitives.push_back(new_drawArray);
|
||||
}
|
||||
}
|
||||
return new_geometry;
|
||||
}
|
||||
|
||||
osg::Geometry* computeThickness(osg::Geometry* orig_geometry, float thickness)
|
||||
{
|
||||
// OSG_NOTICE<<"computeThickness("<<orig_geometry<<")"<<std::endl;
|
||||
@@ -525,9 +310,6 @@ int main(int argc, char** argv)
|
||||
if (!font) return 1;
|
||||
OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
|
||||
|
||||
bool useOldBoundaryCalc = false;
|
||||
while(arguments.read("--old")) useOldBoundaryCalc = true;
|
||||
|
||||
bool useTessellator = false;
|
||||
while(arguments.read("-t") || arguments.read("--tessellate")) { useTessellator = true; }
|
||||
|
||||
@@ -561,15 +343,7 @@ int main(int argc, char** argv)
|
||||
geometry->setColorArray(colours);
|
||||
geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
|
||||
|
||||
osg::Geometry* bevel = 0;
|
||||
if (useOldBoundaryCalc)
|
||||
{
|
||||
bevel = computeBevelEdge(geometry);
|
||||
}
|
||||
else
|
||||
{
|
||||
bevel = computeThickness(geometry, thickness);
|
||||
}
|
||||
osg::Geometry* bevel = computeThickness(geometry, thickness);
|
||||
|
||||
if (bevel) geode->addDrawable(bevel);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user