Show
Ignore:
Timestamp:
07/20/10 12:46:27 (4 years ago)
Author:
robert
Message:

Implemented boundary polygon creation based on the refined boundary segments

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgtext3D/osgtext3D.cpp

    r11680 r11681  
    3838    if (denominator==0.0) 
    3939    { 
    40         OSG_NOTICE<<"computeRayIntersectionPoint()<<denominator==0.0"<<std::endl; 
     40        //OSG_NOTICE<<"computeRayIntersectionPoint()<<denominator==0.0"<<std::endl; 
    4141        // line segments must be parallel. 
    4242        return (a+c)*0.5; 
     
    7575    osg::Vec2 ab(a.x()-b.x(), a.y()-b.y()); 
    7676    osg::Vec2 dc(d.x()-c.x(), d.y()-c.y()); 
    77     float length_ab = ab.normalize(); 
    78     float length_dc = dc.normalize(); 
     77    /*float length_ab =*/ ab.normalize(); 
     78    /*float length_dc =*/ dc.normalize(); 
    7979 
    8080    float e = dc.y() - ab.y(); 
     
    103103    if (bisector_abcd==bisector_cdef) 
    104104    { 
    105         OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl; 
    106         OSG_NOTICE<<"   bisectors parallel, thickness = "<<FLT_MAX<<std::endl; 
     105        //OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl; 
     106        //OSG_NOTICE<<"   bisectors parallel, thickness = "<<FLT_MAX<<std::endl; 
    107107        return FLT_MAX; 
    108108    } 
     
    113113    if (cd_length==0) 
    114114    { 
    115         OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl; 
    116         OSG_NOTICE<<"   segment length==0, thickness = "<<FLT_MAX<<std::endl; 
     115        //OSG_NOTICE<<"computeBisectorIntersector(["<<a<<"], ["<<b<<"], ["<<c<<"], ["<<d<<"], ["<<e<<"], ["<<f<<"[)"<<std::endl; 
     116        //OSG_NOTICE<<"   segment length==0, thickness = "<<FLT_MAX<<std::endl; 
    117117        return FLT_MAX; 
    118118    } 
     
    142142        if ((*_vertices)[start]==(*_vertices)[start+count-1]) 
    143143        { 
    144             OSG_NOTICE<<"Boundary is a line loop"<<std::endl; 
     144            // OSG_NOTICE<<"Boundary is a line loop"<<std::endl; 
    145145        } 
    146146        else 
     
    195195    void removeAllSegmentsBelowThickness(float targetThickness) 
    196196    { 
    197         OSG_NOTICE<<"removeAllSegmentsBelowThickness("<<targetThickness<<")"<<std::endl; 
     197        // OSG_NOTICE<<"removeAllSegmentsBelowThickness("<<targetThickness<<")"<<std::endl; 
    198198        for(;;) 
    199199        { 
     
    202202            if (!findMinThickness(minThickness_i,minThickness)) break; 
    203203 
    204             OSG_NOTICE<<"  removing segment _segments["<<minThickness_i<<"] ("<<_segments[minThickness_i].first<<", "<<_segments[minThickness_i].second<<" with thickness="<<minThickness<<" "<<std::endl; 
     204            // OSG_NOTICE<<"  removing segment _segments["<<minThickness_i<<"] ("<<_segments[minThickness_i].first<<", "<<_segments[minThickness_i].second<<" with thickness="<<minThickness<<" "<<std::endl; 
    205205            _segments.erase(_segments.begin()+minThickness_i); 
    206206        } 
     207    } 
     208 
     209    osg::Vec3 computeBisectorPoint(unsigned int i, float targetThickness) 
     210    { 
     211        Segment& seg_before = _segments[ (i+_segments.size()-1) % _segments.size() ]; 
     212        Segment& seg_target = _segments[ (i) % _segments.size() ]; 
     213        osg::Vec3& a = (*_vertices)[seg_before.first]; 
     214        osg::Vec3& b = (*_vertices)[seg_before.second]; 
     215        osg::Vec3& c = (*_vertices)[seg_target.first]; 
     216        osg::Vec3& d = (*_vertices)[seg_target.second]; 
     217        osg::Vec3 intersection_abcd = computeIntersectionPoint(a,b,c,d); 
     218        osg::Vec3 bisector_abcd = computeBisectorNormal(a,b,c,d); 
     219        osg::Vec3 ab_sidevector(b.y()-a.y(), a.x()-b.x(), 0.0); 
     220        ab_sidevector.normalize(); 
     221        float scale_factor = 1.0/ (bisector_abcd*ab_sidevector); 
     222        osg::Vec3 new_vertex = intersection_abcd + bisector_abcd*(scale_factor*targetThickness); 
     223 
     224        // OSG_NOTICE<<"bisector_abcd = "<<bisector_abcd<<", ab_sidevector="<<ab_sidevector<<", b-a="<<b-a<<", scale_factor="<<scale_factor<<std::endl; 
     225 
     226        new_vertex.z() += 0.5f; 
     227        return new_vertex; 
     228    } 
     229 
     230    void addBoundaryToGeometry(osg::Geometry* geometry, float targetThickness) 
     231    { 
     232        if (_segments.empty()) return; 
     233 
     234        if (geometry->getVertexArray()==0) geometry->setVertexArray(new osg::Vec3Array); 
     235        osg::Vec3Array* new_vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); 
     236 
     237        unsigned int first = new_vertices->size(); 
     238        unsigned int count = 0; 
     239 
     240        // reserve enough space in the vertex array to accomodate the vertices associated with the segments 
     241        // new_vertices->reserve(new_vertices->size()+_segments.size()+1); 
     242 
     243        // create vertices 
     244        unsigned int previous_second = _segments[0].second; 
     245        osg::Vec3 newPoint = computeBisectorPoint(0, targetThickness); 
     246        new_vertices->push_back(newPoint); 
     247        ++count; 
     248 
     249        for(unsigned int i=1; i<_segments.size(); ++i) 
     250        { 
     251            previous_second = _segments[i].second; 
     252            newPoint = computeBisectorPoint(i, targetThickness); 
     253            new_vertices->push_back(newPoint); 
     254            ++count; 
     255        } 
     256 
     257        // repeat the first point to make it a full closed loop 
     258        new_vertices->push_back((*new_vertices)[first]); 
     259        ++count; 
     260 
     261        // add DrawArrays primitive set for polygon 
     262        if (count!=0) geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, first, count)); 
    207263    } 
    208264 
     
    223279void computeBoundaryAngles(osg::Vec3Array& vertices, unsigned int start, unsigned int count) 
    224280{ 
    225     OSG_NOTICE<<"computeBoundaryAngles("<<vertices.size()<<", "<<start<<", "<<count<<")"<<std::endl; 
     281    //OSG_NOTICE<<"computeBoundaryAngles("<<vertices.size()<<", "<<start<<", "<<count<<")"<<std::endl; 
    226282    if (vertices[start+count-1]==vertices[start]) 
    227283    { 
    228         OSG_NOTICE<<"is a line loop"<<std::endl; 
     284        // OSG_NOTICE<<"is a line loop"<<std::endl; 
    229285    } 
    230286    else 
     
    352408{ 
    353409 
    354     OSG_NOTICE<<"computeBevelEdge("<<orig_vertices.size()<<", "<<start<<", "<<count<<")"<<std::endl; 
     410    // OSG_NOTICE<<"computeBevelEdge("<<orig_vertices.size()<<", "<<start<<", "<<count<<")"<<std::endl; 
    355411    if (orig_vertices[start+count-1]==orig_vertices[start]) 
    356412    { 
    357         OSG_NOTICE<<"is a line loop"<<std::endl; 
     413        // OSG_NOTICE<<"is a line loop"<<std::endl; 
    358414    } 
    359415    else 
     
    379435osg::Geometry* computeBevelEdge(osg::Geometry* orig_geometry) 
    380436{ 
    381     OSG_NOTICE<<"computeBoundaryAngles("<<orig_geometry<<")"<<std::endl; 
     437    // OSG_NOTICE<<"computeBoundaryAngles("<<orig_geometry<<")"<<std::endl; 
    382438    osg::Vec3Array* orig_vertices = dynamic_cast<osg::Vec3Array*>(orig_geometry->getVertexArray()); 
    383439    osg::Geometry::PrimitiveSetList& orig_primitives = orig_geometry->getPrimitiveSetList(); 
     
    409465osg::Geometry* computeThickness(osg::Geometry* orig_geometry, float thickness) 
    410466{ 
    411     OSG_NOTICE<<"computeThickness("<<orig_geometry<<")"<<std::endl; 
     467    // OSG_NOTICE<<"computeThickness("<<orig_geometry<<")"<<std::endl; 
    412468    osg::Vec3Array* orig_vertices = dynamic_cast<osg::Vec3Array*>(orig_geometry->getVertexArray()); 
    413469    osg::Geometry::PrimitiveSetList& orig_primitives = orig_geometry->getPrimitiveSetList(); 
     470 
     471    osg::Geometry* new_geometry = new osg::Geometry; 
     472 
     473    osg::Vec4Array* new_colours = new osg::Vec4Array; 
     474    new_colours->push_back(osg::Vec4(1.0,1.0,0.0,1.0)); 
     475    new_geometry->setColorArray(new_colours); 
     476    new_geometry->setColorBinding(osg::Geometry::BIND_OVERALL); 
    414477 
    415478    for(osg::Geometry::PrimitiveSetList::iterator itr = orig_primitives.begin(); 
     
    424487 
    425488            boundary.removeAllSegmentsBelowThickness(thickness); 
    426         } 
    427     } 
    428     return 0; 
    429 } 
    430  
     489            boundary.addBoundaryToGeometry(new_geometry, thickness); 
     490        } 
     491    } 
     492    return new_geometry; 
     493} 
    431494 
    432495int main(int argc, char** argv) 
     
    447510 
    448511    std::string word("This is a simple test"); 
     512 
     513    while(arguments.read("--ascii")) 
     514    { 
     515        word.clear(); 
     516        for(unsigned int c=' '; c<=127;++c) 
     517        { 
     518            word.push_back(c); 
     519        } 
     520    } 
     521 
    449522    while(arguments.read("-w",word)) {} 
    450523 
     
    453526    OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl; 
    454527 
     528    bool useOldBoundaryCalc = false; 
     529    while(arguments.read("--old")) useOldBoundaryCalc = true; 
    455530 
    456531    bool useTessellator = false; 
     
    487562        geometry->setColorBinding(osg::Geometry::BIND_OVERALL); 
    488563 
    489         // computeBoundaryAngles(geometry); 
    490  
    491         osg::Geometry* bevel = computeBevelEdge(geometry); 
    492         geode->addDrawable(bevel); 
    493  
    494         computeThickness(geometry, thickness); 
     564        osg::Geometry* bevel = 0; 
     565        if (useOldBoundaryCalc) 
     566        { 
     567            bevel = computeBevelEdge(geometry); 
     568       } 
     569        else 
     570        { 
     571            bevel = computeThickness(geometry, thickness); 
     572        } 
     573 
     574        if (bevel) geode->addDrawable(bevel); 
    495575 
    496576        if (useTessellator) 
    497577        { 
    498             osgUtil::Tessellator ts; 
    499             ts.setWindingType(osgUtil::Tessellator::TESS_WINDING_POSITIVE); 
    500             ts.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); 
    501             ts.retessellatePolygons(*geometry); 
    502  
    503             ts.retessellatePolygons(*bevel); 
     578            if (geometry) 
     579            { 
     580                osgUtil::Tessellator ts; 
     581                ts.setWindingType(osgUtil::Tessellator::TESS_WINDING_POSITIVE); 
     582                ts.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); 
     583                ts.retessellatePolygons(*geometry); 
     584            } 
     585 
     586            if (bevel) 
     587            { 
     588                osgUtil::Tessellator ts; 
     589                ts.setWindingType(osgUtil::Tessellator::TESS_WINDING_POSITIVE); 
     590                ts.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); 
     591                ts.retessellatePolygons(*bevel); 
     592            } 
    504593 
    505594        }