KiCad PCB EDA Suite
ctriangle2d.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include "ctriangle2d.h"
31 #include <map>
32 #include <boost/smart_ptr/shared_ptr.hpp>
33 #include <boost/smart_ptr/shared_array.hpp>
34 #include <wx/debug.h>
35 
36 #include <wx/glcanvas.h> // CALLBACK definition, needed on Windows
37  // alse needed on OSX to define __DARWIN__
39 #include "../../../3d_fastmath.h"
40 
41 
43  const SFVEC2F& aV1, const SFVEC2F& aV2, const SFVEC2F& aV3, const BOARD_ITEM& aBoardItem )
44  : COBJECT2D( OBJECT2D_TYPE::TRIANGLE, aBoardItem )
45 {
46  p1 = aV1;
47  p2 = aV2;
48  p3 = aV3;
49 
50  // Pre-Calc values
51  m_inv_denominator = 1.0f / ( (p2.y - p3.y) * (p1.x - p3.x) +
52  (p3.x - p2.x) * (p1.y - p3.y));
53  m_p2y_minus_p3y = (p2.y - p3.y);
54  m_p3x_minus_p2x = (p3.x - p2.x);
55  m_p3y_minus_p1y = (p3.y - p1.y);
56  m_p1x_minus_p3x = (p1.x - p3.x);
57 
58  m_bbox.Reset();
59  m_bbox.Union( aV1 );
60  m_bbox.Union( aV2 );
61  m_bbox.Union( aV3 );
64 
65  wxASSERT( m_bbox.IsInitialized() );
66 }
67 
68 
69 bool CTRIANGLE2D::Intersects( const CBBOX2D &aBBox ) const
70 {
71  if( !m_bbox.Intersects( aBBox ) )
72  return false;
74  return true;
75 }
76 
77 
78 bool CTRIANGLE2D::Overlaps( const CBBOX2D &aBBox ) const
79 {
80  // NOT IMPLEMENTED
81  return false;
82 }
83 
84 
85 bool CTRIANGLE2D::Intersect( const RAYSEG2D &aSegRay,
86  float *aOutT,
87  SFVEC2F *aNormalOut ) const
88 {
89  wxASSERT( aOutT );
90  wxASSERT( aNormalOut );
91  return false;
92 }
93 
94 
96 {
97  if( !m_bbox.Intersects( aBBox ) )
99  // !TODO:
101 }
102 
103 
104 bool CTRIANGLE2D::IsPointInside( const SFVEC2F &aPoint ) const
105 {
106  // http://totologic.blogspot.co.uk/2014/01/accurate-point-in-triangle-test.html
107 
108  SFVEC2F point_minus_p3 = aPoint - p3;
109 
110  // barycentric coordinate system
111  const float a = ( m_p2y_minus_p3y * point_minus_p3.x +
112  m_p3x_minus_p2x * point_minus_p3.y ) * m_inv_denominator;
113 
114  if( 0.0f > a || a > 1.0f )
115  return false;
116 
117  const float b = ( m_p3y_minus_p1y * point_minus_p3.x +
118  m_p1x_minus_p3x * point_minus_p3.y ) * m_inv_denominator;
119 
120  if( 0.0f > b || b > 1.0f )
121  return false;
122 
123  const float c = 1.0f - a - b;
124 
125  return 0.0f <= c && c <= 1.0f;
126 }
127 
128 
130  CGENERICCONTAINER2D &aDstContainer,
131  float aBiuTo3DunitsScale ,
132  const BOARD_ITEM &aBoardItem )
133 {
134 
135  aPolyList.CacheTriangulation( false );
136  const double conver_d = (double)aBiuTo3DunitsScale;
137 
138  for( unsigned int j = 0; j < aPolyList.TriangulatedPolyCount(); j++ )
139  {
140  auto triPoly = aPolyList.TriangulatedPolygon( j );
141 
142  for( size_t i = 0; i < triPoly->GetTriangleCount(); i++ )
143  {
144  VECTOR2I a;
145  VECTOR2I b;
146  VECTOR2I c;
147  triPoly->GetTriangle( i, a, b, c );
148 
149  aDstContainer.Add( new CTRIANGLE2D( SFVEC2F( a.x * conver_d,
150  -a.y * conver_d ),
151  SFVEC2F( b.x * conver_d,
152  -b.y * conver_d ),
153  SFVEC2F( c.x * conver_d,
154  -c.y * conver_d ),
155  aBoardItem ) );
156  }
157 
158  }
159 }
INTERSECTION_RESULT
Definition: cobject2d.h:38
void Union(const SFVEC2F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox2d.cpp:95
SFVEC2F p1
Definition: ctriangle2d.h:42
float m_p3x_minus_p2x
Definition: ctriangle2d.h:48
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
CBBOX manages a bounding box defined by two SFVEC2F min max points.
Definition: cbbox2d.h:40
float m_p3y_minus_p1y
Definition: ctriangle2d.h:49
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox2d.cpp:88
INTERSECTION_RESULT IsBBoxInside(const CBBOX2D &aBBox) const override
Function IsBBoxInside.
Definition: ctriangle2d.cpp:95
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
bool IsPointInside(const SFVEC2F &aPoint) const override
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Function Intersect.
Definition: ctriangle2d.cpp:85
SHAPE_POLY_SET.
bool Intersects(const CBBOX2D &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox2d.cpp:213
float m_p1x_minus_p3x
Definition: ctriangle2d.h:50
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
float m_inv_denominator
Definition: ctriangle2d.h:46
void ScaleNextUp()
Function ScaleNextUp scales a bounding box to the next float representation making it larger.
Definition: cbbox2d.cpp:164
CBBOX2D m_bbox
Definition: cobject2d.h:65
void Add(COBJECT2D *aObject)
Definition: ccontainer2d.h:52
SFVEC2F p2
Definition: ctriangle2d.h:43
bool Intersects(const CBBOX2D &aBBox) const override
Function Intersects.
Definition: ctriangle2d.cpp:69
CTRIANGLE2D(const SFVEC2F &aV1, const SFVEC2F &aV2, const SFVEC2F &aV3, const BOARD_ITEM &aBoardItem)
Definition: ctriangle2d.cpp:42
SFVEC2F m_centroid
Definition: cobject2d.h:66
OBJECT2D_TYPE
Definition: cobject2d.h:46
unsigned int TriangulatedPolyCount() const
Returns the number of triangulated polygons
SFVEC2F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
bool Overlaps(const CBBOX2D &aBBox) const override
Function Overlaps.
Definition: ctriangle2d.cpp:78
void CacheTriangulation(bool aPartition=true)
float m_p2y_minus_p3y
Definition: ctriangle2d.h:47
SFVEC2F p3
Definition: ctriangle2d.h:44
const TRIANGULATED_POLYGON * TriangulatedPolygon(int aIndex) const
void Convert_shape_line_polygon_to_triangles(SHAPE_POLY_SET &aPolyList, CGENERICCONTAINER2D &aDstContainer, float aBiuTo3DunitsScale, const BOARD_ITEM &aBoardItem)
Definition: ray.h:110