forked from VTREEM/Carve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeom.cpp
More file actions
160 lines (137 loc) · 4.13 KB
/
Copy pathgeom.cpp
File metadata and controls
160 lines (137 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Begin License:
// Copyright (C) 2006-2011 Tobias Sargeant ([email protected]).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of the GNU General Public
// License version 2.0 as published by the Free Software Foundation
// and appearing in the file LICENSE.GPL2 included in the packaging of
// this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:
#if defined(HAVE_CONFIG_H)
# include <carve_config.h>
#endif
#include <carve/geom.hpp>
namespace carve {
namespace geom {
template<>
vector<2> closestPoint(const tri<2> &tri, const vector<2> &pt) {
return vector<2>::ZERO();
}
template<>
vector<3> closestPoint(const tri<3> &tri, const vector<3> &pt) {
const vector<3> e0 = tri.v[1] - tri.v[0];
const vector<3> e1 = tri.v[2] - tri.v[0];
const vector<3> dp = tri.v[0] - pt;
// triangle is defined by
// T(s,t) = tri.v[0] + e0.s + e1.t; s >= 0, t >= 0, s + t <= 1
// distance from pt to point on triangle plane:
// Q(s, t) = a(s^2) + 2b(st) + c(t^2) + 2d(s) + 2e(t) + f
// grad(Q) = 2(as + bt + d, bs + ct + e)
const double a = dot(e0, e0);
const double b = dot(e1, e0);
const double c = dot(e1, e1);
const double d = dot(e0, dp);
const double e = dot(e1, dp);
const double f = dot(dp, dp);
const double det = a*c - b*b;
double s = b*e - c*d;
double t = b*d - a*e;
/* t */
/* ^ */
/* \4| */
/* \| */
/* \ */
/* |\6 */
/* 1|3\ */
/* ---+--\----> s */
/* 0|2 \5 */
int edge;
if (s+t <= det) { // regions 0, 1, 2, 3
if (s < 0 && t < 0) {
// region 0 - minimum on edge (1) or (2)
if (d < 0) {
// grad(Q)(0,0).(1,0) < 0
edge = 1;
} else {
edge = 2;
}
} else if (s < 0) {
// region 1 - minimum on edge (1)
edge = 1;
} else if (t < 0) {
// region 2 - minimum on edge (2)
edge = 2;
} else {
// region 3 - closest point within triangle bounds.
edge = 0;
}
} else { // regions 4, 5, 6
if (s < 0) {
// region 4 - minimum on edge (1) or (3)
if (-(c+e) < 0) {
// grad(Q)(0,1).(0,-1) < 0
edge = 1;
} else {
edge = 3;
}
} else if (t < 0) {
// region 5 - minimum on edge (2) or (3)
if (-(a+d) < 0) {
// grad(Q)(1,0).(-1,0) < 0
edge = 2;
} else {
edge = 3;
}
} else {
// region 6 - minimum on edge (3)
edge = 3;
}
}
switch (edge) {
case 0: {
s /= det;
t /= det;
break;
}
case 1: {
// edge (1)
// s = 0, t = [0,1]
// :: Q(t) = c(t^2) + 2e(t) + f
// :: dQ/dt = 2ct + 2e
s = 0;
t = math::clamp(-e/c, 0.0, 1.0);
break;
}
case 2: {
// edge (2)
// t = 0, s = [0,1]
// :: Q(s) = a(s^2) + 2d(s) + f
// :: dQ/ds = 2as + 2d
s = math::clamp(-d/a, 0.0, 1.0);
t = 0;
break;
}
case 3: {
// edge (3)
// t = 1 - s, s = [0,1]
// Q(s) = a(s^2) + 2b(s(1-s)) + c((1-s)^2) + 2d(s) + 2e(1-s) + f
// = a(s^2) + 2b(s-s^2) + c(1-2s+s^2) + 2d(s) + 2e(1-s) + f
// = a(s^2) + 2b(s) - 2b(s^2) + c - 2c(s) + c(s^2) + 2d(s) + 2e - 2e(s) + f
// = (a - 2b + c)(s^2) + (2b - 2c + 2d - 2e)(s) + (c + 2e + f)
// dQ/ds = 2(a - 2b + c)s + 2(b - c + d - e)
s = math::clamp((c+e-b-d)/(a-2*b+c), 0.0, 1.0);
t = 1 - s;
break;
}
}
const vector<3> closest = tri.v[0] + e0 * s + e1 * t;
return closest;
}
}
}