-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle3d.tcl
More file actions
308 lines (241 loc) · 8.58 KB
/
Copy pathTriangle3d.tcl
File metadata and controls
308 lines (241 loc) · 8.58 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright (c) 2021-2024 Nicolas ROBERT.
# Distributed under MIT license. Please see LICENSE for details.
namespace eval tomato::mathtriangle {
# Ruff documentation
variable _ruff_preamble "A Class representing a Triangle in 3D space"
}
oo::class create tomato::mathtriangle::Triangle {
variable _a ;
variable _b ;
variable _c ;
constructor {args} {
# Initializes a new Triangle Class.
#
# args - Options described below.
#
# 3 values - [mathpt3d::Point3d]
# list - 3 distinct values that represent the coordinates X, Y, Z.
#
if {[llength $args] != 3} {
error "Must be a list of 3 values... : \$args = [llength $args]"
}
set _triangle {}
foreach val $args {
if {[tomato::helper::TypeOf $val Isa "Point3d"]} {
lappend _triangle $val
} elseif {[string is list $val] && ([llength $val] == 3)} {
lappend _triangle [tomato::mathpt3d::Point3d new {*}$val]
} else {
error "list of 3 values or 'Point3d' class..."
}
}
if {[llength $_triangle] != 3} {
error "Triangle must be a list of 3 values... : $_triangle"
}
if {[tomato::mathpt3d::IsCollinearPoints {*}$_triangle]} {
error "Collinear points..."
}
lassign $_triangle _a _b _c
}
}
oo::define tomato::mathtriangle::Triangle {
method A {} {
# Returns the first point of the Triangle [mathpt3d::Point3d].
return $_a
}
method B {} {
# Returns the second point of the Triangle [mathpt3d::Point3d].
return $_b
}
method C {} {
# Returns the third point of the Triangle [mathpt3d::Point3d].
return $_c
}
method AB {} {
# Gets Length of AB side.
return [$_a DistanceTo $_b]
}
method AC {} {
# Gets Length of AC side.
return [$_a DistanceTo $_c]
}
method BC {} {
# Gets Length of BC side.
return [$_b DistanceTo $_c]
}
method Perimeter {} {
# Gets Perimeter of the triangle.
return [expr {[my AB] + [my BC] + [my AC]}]
}
method Aera {} {
# Gets Aera of the triangle.
set v1 [$_a VectorTo $_b]
set v2 [$_a VectorTo $_c]
return [expr {0.5 * [[$v1 CrossProduct $v2] Length]}]
}
method Normal {} {
# Gets Normal of the triangle [mathvec3d::Vector3d].
set v1 [$_a VectorTo $_b]
set v2 [$_a VectorTo $_c]
return [$v1 CrossProduct $v2]
}
method ToPlane {} {
# Convert triangle to plane object [mathplane::Plane].
return [tomato::mathplane::Plane new $_a [my Normal]]
}
method AngleA {} {
# Gets Angle at the vertex A
#
# Returns The angle in radian between the vectors, with a range between 0° and 180°
set v1 [$_a VectorTo $_b]
set v2 [$_a VectorTo $_c]
return [$v1 AngleTo $v2]
}
method AngleB {} {
# Gets Angle at the vertex B
#
# Returns The angle in radian between the vectors, with a range between 0° and 180°
set v1 [$_b VectorTo $_a]
set v2 [$_b VectorTo $_c]
return [$v1 AngleTo $v2]
}
method AngleC {} {
# Gets Angle at the vertex C
#
# Returns The angle in radian between the vectors, with a range between 0° and 180°
set v1 [$_c VectorTo $_a]
set v2 [$_c VectorTo $_b]
return [$v1 AngleTo $v2]
}
method BisectorA {} {
# Angle bisector at the vertex A
#
# Returns [mathline3d::Line3d]
set p [$_b + [[$_c - $_b] / [expr {1.0 + [my AC] / [my AB]}]]]
return [tomato::mathline3d::Line3d new $_a $p]
}
method BisectorB {} {
# Angle bisector at the vertex B
#
# Returns [mathline3d::Line3d]
set p [$_c + [[$_a - $_c] / [expr {1.0 + [my AB] / [my BC]}]]]
return [tomato::mathline3d::Line3d new $_b $p]
}
method BisectorC {} {
# Angle bisector at the vertex C
#
# Returns [mathline3d::Line3d]
set p [$_a + [[$_b - $_a] / [expr {1.0 + [my BC] / [my AC]}]]]
return [tomato::mathline3d::Line3d new $_c $p]
}
method Centroid {} {
# Gets Centroid of the triangle
#
# Returns [mathpt3d::Point3d]
return [tomato::mathpt3d::Centroid [list $_a $_b $_c]]
}
method IntersectionWith {obj {tolerance $::tomato::helper::TolGeom}} {
# Finds the intersection...
# [Möller–Trumbore_intersection_algorithm](https://en.wikipedia.org/wiki/Möller–Trumbore_intersection_algorithm)
#
# obj - Options described below.
#
# Ray - [mathray3d::Ray3d]
# Line - [mathline3d::Line3d]
# tolerance - A tolerance (epsilon) to account for floating point error
#
# Returns nothing is no intersection, A [mathpt3d::Point3d] if intersection.
if {[llength [info level 0]] < 4} {
set tolerance $::tomato::helper::TolGeom
}
switch -glob [$obj GetType] {
*Ray3d {
set ip [tomato::mathplane::IntersectionWithRay [my ToPlane] $obj $tolerance]
}
*Line3d {
set ip [tomato::mathplane::IntersectionWithLine [my ToPlane] $obj $tolerance]
}
default {
#ruff
# An error exception is raised if $obj is not as described above.
error "Obj must be Line3d or Ray3d..."
}
}
# get intersect point of line with triangle plane
if {$ip eq ""} {
# no intersection...
return {}
}
set u [$_b VectorTo $_a]
set v [$_c VectorTo $_a]
set w [$ip VectorTo $_a]
set uu [$u DotProduct $u]
set uv [$u DotProduct $v]
set vv [$v DotProduct $v]
set wu [$w DotProduct $u]
set wv [$w DotProduct $v]
set D [expr {($uv * $uv) - ($uu * $vv)}]
# ip is outside T ?
set s [expr {(($uv * $wv) - ($vv * $wu)) / $D}]
if {$s < 0.0 || $s > 1.0} {
return ""
}
# ip is outside T ?
set t [expr {(($uv * $wu) - ($uu * $wv)) / $D}]
if {$t < 0.0 || ($s + $t) > 1.0} {
return ""
}
return $ip
}
method == {other {tolerance $::tomato::helper::TolEquals}} {
# Gets value that indicates whether each pair of elements in two specified points is equal.
#
# other - The second triangle [Triangle] to compare.
# tolerance - A tolerance (epsilon) to adjust for floating point error.
#
# Returns `True` if the triangles are the same. Otherwise `False`.
if {[llength [info level 0]] < 4} {
set tolerance $::tomato::helper::TolEquals
}
return [expr {[tomato::mathtriangle::Equals [self] $other $tolerance]}]
}
method != {other {tolerance $::tomato::helper::TolEquals}} {
# Gets value that indicates whether any pair of elements in two specified points is not equal.
#
# other - The second triangle [Triangle] to compare.
# tolerance - A tolerance (epsilon) to adjust for floating point error.
#
# Returns `True` if the triangles are different. Otherwise `False`.
if {[llength [info level 0]] < 4} {
set tolerance $::tomato::helper::TolEquals
}
return [expr {![tomato::mathtriangle::Equals [self] $other $tolerance]}]
}
method GetType {} {
# Gets the name of class.
return [tomato::helper::TypeClass [self]]
}
export A B C AB AC BC Perimeter Aera Normal ToPlane
export AngleA AngleB AngleC Centroid IntersectionWith
export BisectorA BisectorB BisectorC GetType
export == !=
}
proc tomato::mathtriangle::Equals {triangle other tolerance} {
# Gets a value to indicate if a pair of triangles are equal
#
# triangle - First input triangle [Triangle]
# other - Second input triangle [Triangle]
# tolerance - A tolerance (epsilon) to adjust for floating point error
#
# Returns `True` if the triangles are equal, otherwise false.
#
# See : methods == !=
if {$tolerance < 0} {
#ruff
# An error exception is raised if tolerance (epsilon) < 0.
error "epsilon < 0"
}
return [expr {[[$triangle Centroid] == [$other Centroid] $tolerance] &&
(([$triangle Aera] - [$other Aera]) < $tolerance)
}]
}