1 /*
2 Copyright (c) 2013-2017 Timur Gafarov 
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dmech.clipping;
30 
31 import dlib.math.vector;
32 
33 /*
34  * 2D feature clipping.
35  * Feature is a quad, triangle, line segment or point 
36  */
37 
38 struct Feature
39 {
40     Vector2f[4] vertices;
41     uint numVertices = 0;
42 }
43 
44 bool pointInPolygon(Vector2f[] vert, uint len, Vector2f point)
45 {
46     int i, j = 0;
47     bool c = false;
48     for (i = 0, j = len - 1; i < len; j = i++)
49     {
50         if ( ((vert[i].y > point.y) != (vert[j].y > point.y)) &&
51              (point.x < (vert[j].x - vert[i].x) * (point.y - vert[i].y) / (vert[j].y - vert[i].y) + vert[i].x) )
52             c = !c;
53     }
54     return c;
55 }
56 
57 void featurePointsIn(Feature f1, Feature f2, ref bool[4] res)
58 {
59     for (uint i = 0; i < f1.numVertices; i++)
60         res[i] = pointInPolygon(f2.vertices, f2.numVertices, f1.vertices[i]);        
61 }
62 
63 bool allIsTrue(ref bool[4] arr, uint len)
64 {
65     for (uint i = 0; i < len; i++)
66         if (!arr[i]) 
67             return false;
68     return true;
69 }
70 
71 // Detect intersection of two line segments
72 bool segmentIsec(
73     Vector2f start1, Vector2f end1, 
74     Vector2f start2, Vector2f end2, 
75     out Vector2f out_intersection)
76 {
77     Vector2f dir1 = end1 - start1;
78     Vector2f dir2 = end2 - start2;
79 
80     float a1 = -dir1.y;
81     float b1 = +dir1.x;
82     float d1 = -(a1*start1.x + b1*start1.y);
83 
84     float a2 = -dir2.y;
85     float b2 = +dir2.x;
86     float d2 = -(a2*start2.x + b2*start2.y);
87 
88     float seg1_line2_start = a2*start1.x + b2*start1.y + d2;
89     float seg1_line2_end = a2*end1.x + b2*end1.y + d2;
90 
91     float seg2_line1_start = a1*start2.x + b1*start2.y + d1;
92     float seg2_line1_end = a1*end2.x + b1*end2.y + d1;
93 
94     if (seg1_line2_start * seg1_line2_end >= 0 || 
95         seg2_line1_start * seg2_line1_end >= 0) 
96         return false;
97 
98     float u = seg1_line2_start / (seg1_line2_start - seg1_line2_end);
99     out_intersection = start1 + dir1 * u;
100 
101     return true;
102 }
103 
104 T distancesqr2(T) (Vector!(T,2) a, Vector!(T,2) b)
105 body
106 {
107     T dx = a.x - b.x;
108     T dy = a.y - b.y;
109     return ((dx * dx) + (dy * dy));
110 }
111 
112 // Perform clipping of two features
113 bool clip(
114     Feature f1,
115     Feature f2,
116     out Vector2f[8] outPts, 
117     out uint numOutPts) 
118 {
119 /*
120     if (f1.numVertices == 1)
121     {
122         outPts[0] = f1.vertices[0];
123         numOutPts = 1;
124     }
125     if (f2.numVertices == 1)
126     {
127         outPts[0] = f2.vertices[0];
128         numOutPts = 1;
129     }
130 */
131 
132     // Check if features are almost the same
133     if (f1.numVertices == f2.numVertices)
134     {
135         bool same = false;
136         for (uint i = 0; i < f1.numVertices; i++)
137             same = same || distancesqr2(f1.vertices[i], f2.vertices[i]) < 0.001f;
138         if (same)
139         {
140             // result is f1
141             for (uint i = 0; i < f1.numVertices; i++)
142                 outPts[i] = f1.vertices[i];
143             numOutPts = f1.numVertices;
144             return true;
145         }
146     }
147 
148     // Check if one feature fully encloses another
149     bool[4] r1;
150     bool[4] r2;
151 
152     if (f2.numVertices > 2)
153         featurePointsIn(f1, f2, r1);
154     if (f1.numVertices > 2)
155         featurePointsIn(f2, f1, r2);
156 
157     if (allIsTrue(r1, f1.numVertices))
158     {
159         // f2 fully encloses f1, result is f1
160         for (uint i = 0; i < f1.numVertices; i++)
161             outPts[i] = f1.vertices[i];
162         numOutPts = f1.numVertices;
163         return true;
164     }
165 
166     if (allIsTrue(r2, f2.numVertices))
167     {
168         // f1 fully encloses f2, result is f2
169         for (uint i = 0; i < f2.numVertices; i++)
170             outPts[i] = f2.vertices[i];
171         numOutPts = f2.numVertices;
172         return true;
173     }
174 
175     // Add enclosed points to result
176     numOutPts = 0;
177     foreach(i, r; r1)
178     if (r)
179     {
180         outPts[numOutPts] = f1.vertices[i];
181         numOutPts++;
182     }
183     foreach(i, r; r2)
184     if (r)
185     {
186         outPts[numOutPts] = f2.vertices[i];
187         numOutPts++;
188     }
189 
190     // Check one feature's edges against another's
191     // TODO: check only those edges that belong to enclosed points
192     for (uint i = 0; i < f1.numVertices; i++)
193     for (uint j = 0; j < f2.numVertices; j++)
194     {
195         uint i2 = i + 1;
196         if (i2 == f1.numVertices) i2 = 0;
197         uint j2 = j + 1;
198         if (j2 == f2.numVertices) j2 = 0;
199 
200         Vector2f a = f1.vertices[i];
201         Vector2f b = f1.vertices[i2];
202         Vector2f c = f2.vertices[j];
203         Vector2f d = f2.vertices[j2];
204 
205         Vector2f ip;
206         if (segmentIsec(a, b, c, d, ip))
207         {
208             if (numOutPts < 8)
209             {
210                 outPts[numOutPts] = ip;
211                 numOutPts++;
212             }
213         }
214     }
215 
216     return (numOutPts > 0);
217 }
218