4 #ifndef tools_clist_contour
5 #define tools_clist_contour
30 virtual void ExportLine(
int iPlane,
int x1,
int y1,
int x2,
int y2);
58 void DumpPlane(
unsigned int iPlane)
const;
78 typedef unsigned int UINT;
81 static const char* _TT(
const char*
what) {
return what;}
83 static void _TRACE_(
const char* ,...) {
89 static void _PROBLEM_(
const char*
what) {::printf(
"%s",
what);}
91 static bool _ASSERT_RET_(
bool what,
const char* cmt) {
93 ::printf(
"debug : ListContour : assert failure in %s\n",cmt);
99 static bool _ASSERT_MERGE_RET_(
bool what,
const char* cmt,
cline_strip* pStrip2) {
101 ::printf(
"debug : ListContour : assert failure in %s\n",cmt);
128 cline_strip_list::iterator pos;
131 if (!m_vStripLists.empty())
138 for (pos = m_vStripLists[i].
begin(); pos!=m_vStripLists[i].end() ; pos++)
141 _ASSERTP_(pStrip,
"clist_contour::InitMemory::1");
146 m_vStripLists[i].clear();
157 cline_strip_list::iterator pos;
162 for (i=0;i<m_vStripLists.size();i++)
164 for (pos=m_vStripLists[i].
begin(); pos!=m_vStripLists[i].end();pos++)
167 _ASSERTP_(pStrip,
"clist_contour::CleanMemory");
171 m_vStripLists[i].clear();
177 _ASSERT_(iPlane>=0,
"clist_contour::ExportLine::0");
186 cline_strip_list::iterator pos;
188 for(pos=m_vStripLists[iPlane].
begin(); pos!=m_vStripLists[iPlane].end() && !added; pos++)
191 _ASSERTP_(pStrip,
"clist_contour::ExportLine::2");
193 if (i1==pStrip->front())
195 pStrip->insert(pStrip->begin(),i2);
198 if (i1==pStrip->back())
200 pStrip->insert(pStrip->end(),i2);
203 if (i2==pStrip->front())
205 pStrip->insert(pStrip->begin(),i1);
208 if (i2==pStrip->back())
210 pStrip->insert(pStrip->end(),i1);
217 pStrip->insert(pStrip->begin(),i1);
218 pStrip->insert(pStrip->end(),i2);
219 m_vStripLists[iPlane].insert(m_vStripLists[iPlane].
begin(),pStrip);
225 cline_strip::iterator pos;
226 cline_strip::reverse_iterator rpos;
228 if (pStrip2->empty())
231 double x[4], y[4], weldDist;
233 index = pStrip1->front();
236 index = pStrip1->back();
239 index = pStrip2->front();
242 index = pStrip2->back();
248 if (((x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2])< weldDist)
252 for (pos=pStrip2->begin(); pos!=pStrip2->end();pos++)
255 if(!_ASSERT_RET_(index>=0,
"clist_contour::ForceMerge::0"))
return false;
256 pStrip1->insert(pStrip1->end(),index);
262 if (((x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0])< weldDist)
265 for (rpos=pStrip2->rbegin(); rpos!=pStrip2->rend();rpos++)
268 if(!_ASSERT_RET_(index>=0,
"clist_contour::ForceMerge::1"))
return false;
269 pStrip1->insert(pStrip1->begin(),index);
275 if (((x[1]-x[3])*(x[1]-x[3])+(y[1]-y[3])*(y[1]-y[3])< weldDist)
278 for (rpos=pStrip2->rbegin(); rpos!=pStrip2->rend();rpos++)
281 if(!_ASSERT_RET_(index>=0,
"clist_contour::ForceMerge::2"))
return false;
282 pStrip1->insert(pStrip1->end(),index);
288 if (((x[0]-x[2])*(x[0]-x[2])+(y[0]-y[2])*(y[0]-y[2])< weldDist)
291 for (pos=pStrip2->begin(); pos!=pStrip2->end();pos++)
294 if(!_ASSERT_RET_(index>=0,
"clist_contour::ForceMerge::3"))
return false;
295 pStrip1->insert(pStrip1->begin(),index);
306 cline_strip::iterator pos;
307 cline_strip::reverse_iterator rpos;
308 if (pStrip2->empty())
314 if (pStrip2->front()==pStrip1->front())
317 pStrip2->pop_front();
319 for (pos=pStrip2->begin(); pos!=pStrip2->end();pos++)
322 if(!_ASSERT_MERGE_RET_(index>=0,
"clist_contour::MergeStrips::0",pStrip2))
return false;
323 pStrip1->insert(pStrip1->begin(),index);
329 if (pStrip2->front()==pStrip1->back())
331 pStrip2->pop_front();
333 for (pos=pStrip2->begin(); pos!=pStrip2->end();pos++)
336 if(!_ASSERT_MERGE_RET_(index>=0,
"clist_contour::MergeStrips::1",pStrip2))
return false;
337 pStrip1->insert(pStrip1->end(),index);
343 if (pStrip2->back()==pStrip1->front())
347 for (rpos=pStrip2->rbegin(); rpos!=pStrip2->rend();rpos++)
350 if(!_ASSERT_MERGE_RET_(index>=0,
"clist_contour::MergeStrips::2",pStrip2))
return false;
351 pStrip1->insert(pStrip1->begin(),index);
357 if (pStrip2->back()==pStrip1->back())
361 for (rpos=pStrip2->rbegin(); rpos!=pStrip2->rend();rpos++)
364 if(!_ASSERT_MERGE_RET_(index>=0,
"clist_contour::MergeStrips::3",pStrip2))
return false;
365 pStrip1->insert(pStrip1->end(),index);
379 cline_strip_list::iterator pos,pos2;
385 if(!_ASSERT_RET_(m_vStripLists.size() ==
get_number_of_planes(),
"clist_contour::compact_strips ::0"))
return false;
396 if(!_ASSERT_RET_(newList.empty(),
"clist_contour::compact_strips ::1"))
return false;
397 for (pos=m_vStripLists[i].
begin(); pos!=m_vStripLists[i].end();pos++)
400 for (pos2=newList.begin(); pos2!=newList.end();pos2++)
412 newList.insert(newList.begin(),pStrip);
417 m_vStripLists[i].clear();
420 for (pos2=newList.begin(); pos2 != newList.end(); pos2++)
423 cline_strip::iterator pos1 = pStrip2->begin(),pos3;
424 while (pos1!=pStrip2->end())
428 if (pos3==pStrip2->end())
break;
429 if ( (*pos1) == (*pos3))
440 pStrip2->erase(pos3);
447 if (pStrip2->size()!=1)
448 m_vStripLists[i].insert(m_vStripLists[i].
begin(),pStrip2 );
458 if (m_vStripLists[i].empty())
465 Nstrip = m_vStripLists[i].size();
466 std::vector<bool> closed(Nstrip);
470 for(pos2 = m_vStripLists[i].
begin(), j=0, count=0; pos2 != m_vStripLists[i].end(); pos2++, j++)
475 if (pStrip->front() != pStrip->back())
477 index = pStrip->front();
479 index = pStrip->back();
483 if ((x*x+y*y < weldDist) ||
492 pStrip->push_back(pStrip->front());
510 pos = m_vStripLists[i].begin();
511 for(j=0;j<Nstrip;j++)
513 if (closed[j] ==
false )
516 newList.insert(newList.begin(),pStrip);
517 pos = m_vStripLists[i].erase(pos);
524 while(newList.size()>1)
526 pStripBase = newList.front();
533 pos = newList.begin();
534 for(pos++; pos!=newList.end();)
537 changed =
ForceMerge(pStripBase,pStrip,planeHeight);
542 pos = newList.erase(pos);
549 index = pStripBase->front();
551 index = pStripBase->back();
555 if ((x*x+y*y < weldDist) ||
557 get_xi(pStripBase->back()),
558 get_yi(pStripBase->front()),
559 get_yi(pStripBase->back()),
564 if ((x!=0) || (y!=0)) {
565 pStripBase->push_back(pStripBase->front());
568 m_vStripLists[i].insert(m_vStripLists[i].
begin(),pStripBase);
575 _TRACE_(_TT(
"# open strip ends on boundary, continue.\n"));
576 m_vStripLists[i].insert(m_vStripLists[i].
begin(),pStripBase);
581 _PROBLEM_(_TT(
"unpaird open strip at 1!\n"));
589 if (newList.size() ==1)
591 pStripBase = newList.front();
592 index = pStripBase->front();
594 index = pStripBase->back();
598 if (x*x+y*y < 3*weldDist)
600 m_vStripLists[i].insert(m_vStripLists[i].
begin(),pStripBase);
605 _TRACE_(_TT(
"# open strip ends on boundary, continue.\n"));
606 m_vStripLists[i].insert(m_vStripLists[i].
begin(),pStripBase);
611 _PROBLEM_(_TT(
"unpaird open strip at 2!\n"));
623 pos = m_vStripLists[i].begin();
624 for(j=0;j<Nstrip;j++)
626 if (closed[j] ==
false )
633 index = pStripBase->front();
635 index = pStripBase->back();
639 if (x*x+y*y < 2*weldDist)
642 pStripBase->push_back(pStripBase->front());
646 _TRACE_(_TT(
"# open strip ends on boundary, continue.\n"));
647 pStripBase->push_back(pStripBase->front());
651 _TRACE_(_TT(
"unpaird open strip at 3!"));
667 int index = pStrip->front();
675 index = pStrip->back();
688 cline_strip_list::const_iterator pos;
694 _TRACE_(_TT(
"Level : %d"),
get_plane(iPlane));
696 _TRACE_(_TT(
"Number of strips : %d\r\n"),m_vStripLists[iPlane].
size());
697 _TRACE_(_TT(
"i np start end xstart ystart xend yend\r\n"));
698 for (pos = m_vStripLists[iPlane].
begin(), i=0; pos != m_vStripLists[iPlane].end(); pos++, i++)
701 _ASSERTP_(pStrip,
"clist_contour::DumpPlane::1");
702 _TRACE_(_TT(
"%d %d %d %d %g %g %g %g\r\n"),i,pStrip->size(),pStrip->front(),pStrip->back(),
712 double Ar = 0, x, y, x0,y0,x1, y1;
715 cline_strip::iterator pos;
723 for(;pos!=Line->end();pos++)
729 Ar += (y1-y)*(x1+x)-(x1-x)*(y1+y);
737 Ar += (y0-y)*(x0+x)-(x0-x)*(y0+y);
742 _TRACE_(_TT(
"# open curve!\n"));
752 cline_strip::iterator pos;
755 for(pos = pLine->begin(); pos!=pLine->end(); pos++)
760 if (fabs(x) > R || fabs(y) > R)
763 return (
double)count/pLine->size();
799 return &m_vStripLists[iPlane];
821 double distToNext =0;
832 if ((aYback-aYfront) == 0) {
841 if (aXfront > aXback ) {
846 for(
double check=aXfront+
m_dDx;
853 if ((av>distToNext) && (ap>distToNext) && (eg>distToNext)) {
855 }
else if ((av<0) && (ap<0) && (eg<0)) {
860 }
else if ((aXback-aXfront) == 0) {
868 if (aYfront > aYback ) {
874 for(
double check=aYfront+
m_dDy;
880 if ((av>distToNext) && (ap>distToNext) && (eg>distToNext)) {
882 }
else if ((av<0) && (ap<0) && (eg<0)) {