1 # Copyright (c) 2016 OPEN CASCADE SAS
3 # This file is part of Open CASCADE Technology software library.
5 # This library is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License version 2.1 as published
7 # by the Free Software Foundation, with special exception defined in the file
8 # OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 # distribution for complete text of the license and disclaimer of any warranty.
11 # Alternatively, this file may be used under the terms of Open CASCADE
12 # commercial license or contractual agreement.
14 # Created by: M.Sazonov
16 # Working with vectors and various measurements
18 # [2d] point is represented by (two/three) coords
19 # [2d] vector is represented by (two/three) coords
20 # plane is represented by an origin point and a normal vector
21 # [2d] line is represented by an origin point and a vector
23 help vec {vec x1 y1 z1 x2 y2 z2
24 returns coordinates of vector between two points\
25 } {Vector and measurement Commands}
27 proc vec {x1 y1 z1 x2 y2 z2} {
28 uplevel list [dval ($x2)-($x1)] [dval ($y2)-($y1)] [dval ($z2)-($z1)]
31 help 2dvec {2dvec x1 y1 x2 y2
32 returns coordinates of 2D vector between two 2D points\
33 } {Vector and measurement Commands}
35 proc 2dvec {x1 y1 x2 y2} {
36 uplevel list [dval ($x2)-($x1)] [dval ($y2)-($y1)]
39 help pln {pln x1 y1 z1 x2 y2 z2 x3 y3 z3
40 returns plane built on three points\
41 } {Vector and measurement Commands}
43 proc pln {x1 y1 z1 x2 y2 z2 x3 y3 z3} {
44 set v12 [uplevel eval norm [vec $x1 $y1 $z1 $x2 $y2 $z2]]
45 set v13 [uplevel eval norm [vec $x1 $y1 $z1 $x3 $y3 $z3]]
46 set vn [eval cross $v12 $v13]
47 set N [eval module $vn]
49 puts "points are on a line"
52 concat $x1 $y1 $z1 [eval norm $vn]
55 help module {module x y z
56 returns module of a vector\
57 } {Vector and measurement Commands}
60 uplevel dval sqrt(($x)*($x)+($y)*($y)+($z)*($z))
63 help 2dmodule {2dmodule x y
64 returns module of a 2D vector\
65 } {Vector and measurement Commands}
68 uplevel dval sqrt(($x)*($x)+($y)*($y))
72 returns unified vector from a given vector\
73 } {Vector and measurement Commands}
76 set N [uplevel dval sqrt(($x)*($x)+($y)*($y)+($z)*($z))]
77 list [uplevel dval ($x)/$N] [uplevel dval ($y)/$N] [uplevel dval ($z)/$N]
80 help 2dnorm {2dnorm x y
81 returns unified vector from a given 2D vector\
82 } {Vector and measurement Commands}
85 set N [uplevel dval sqrt(($x)*($x)+($y)*($y))]
86 list [uplevel dval ($x)/$N] [uplevel dval ($y)/$N]
89 help inverse {inverse x y z
90 returns inversed vector\
91 } {Vector and measurement Commands}
93 proc inverse {x y z} {
94 list [uplevel dval -$x] [uplevel dval -$y] [uplevel dval -$z]
97 help 2dinverse {2dinverse x y
98 returns inversed 2D vector\
99 } {Vector and measurement Commands}
101 proc 2dinverse {x y} {
102 list [uplevel dval -$x] [uplevel dval -$y]
105 help 2dort {2dort x y
106 returns 2D vector rotated on 90 degrees\
107 } {Vector and measurement Commands}
110 list [uplevel dval -$y] [uplevel dval $x]
113 help distpp {distpp x1 y1 z1 x2 y2 z2
114 returns distance between two points\
115 } {Vector and measurement Commands}
117 proc distpp {x1 y1 z1 x2 y2 z2} {
118 eval module [uplevel vec $x1 $y1 $z1 $x2 $y2 $z2]
121 help 2ddistpp {2ddistpp x1 y1 x2 y2
122 returns distance between two 2D points\
123 } {Vector and measurement Commands}
125 proc 2ddistpp {x1 y1 x2 y2} {
126 eval 2dmodule [uplevel 1 2dvec $x1 $y1 $x2 $y2]
129 help distplp {distplp xo yo zo dx dy dz xp yp zp
130 returns distance between plane and point\
131 } {Vector and measurement Commands}
133 proc distplp {xo yo zo dx dy dz xp yp zp} {
134 set vop [uplevel vec $xo $yo $zo $xp $yp $zp]
135 set vn [uplevel norm $dx $dy $dz]
139 help distlp {distlp xo yo zo dx dy dz xp yp zp
140 returns distance between line and point\
141 } {Vector and measurement Commands}
143 proc distlp {xo yo zo dx dy dz xp yp zp} {
144 set vop [uplevel vec $xo $yo $zo $xp $yp $zp]
145 set vl [uplevel norm $dx $dy $dz]
146 eval module [eval cross $vl $vop]
149 help 2ddistlp {2ddistlp xo yo dx dy xp yp
150 returns distance between 2D line and point\
151 } {Vector and measurement Commands}
153 proc 2ddistlp {xo yo dx dy xp yp} {
154 set vop [uplevel 1 2dvec $xo $yo $xp $yp]
155 set vl [uplevel 1 2dnorm $dx $dy]
156 eval 2dcross $vl $vop
159 help distppp {distppp x1 y1 z1 x2 y2 z2 x3 y3 z3
160 returns deviation of point p2 from segment p1-p3\
161 } {Vector and measurement Commands}
163 proc distppp {x1 y1 z1 x2 y2 z2 x3 y3 z3} {
164 set vop [uplevel vec $x1 $y1 $z1 $x2 $y2 $z2]
165 set vl [uplevel eval norm [vec $x1 $y1 $z1 $x3 $y3 $z3]]
166 eval module [eval cross $vl $vop]
169 help 2ddistppp {2ddistppp x1 y1 x2 y2 x3 y3
170 returns deviation of 2D point p2 from segment p1-p3 (sign shows the side)\
171 } {Vector and measurement Commands}
173 proc 2ddistppp {x1 y1 x2 y2 x3 y3} {
174 set vop [uplevel 1 2dvec $x1 $y1 $x2 $y2]
175 set vl [uplevel eval 2dnorm [2dvec $x1 $y1 $x3 $y3]]
176 eval 2dcross $vl $vop
179 help barycen {barycen x1 y1 z1 x2 y2 z2 par
180 returns point of a given parameter between two points\
181 } {Vector and measurement Commands}
183 proc barycen {x1 y1 z1 x2 y2 z2 par} {
184 uplevel list [dval ($x1)*(1-($par))+($x2)*($par)]\
185 [dval ($y1)*(1-($par))+($y2)*($par)]\
186 [dval ($z1)*(1-($par))+($z2)*($par)]
189 help 2dbarycen {2dbarycen x1 y1 x2 y2 par
190 returns 2D point of a given parameter between two points\
191 } {Vector and measurement Commands}
193 proc 2dbarycen {x1 y1 x2 y2 par} {
194 uplevel list [dval ($x1)*(1-($par))+($x2)*($par)]\
195 [dval ($y1)*(1-($par))+($y2)*($par)]
198 help cross {cross x1 y1 z1 x2 y2 z2
199 returns cross product of two vectors\
200 } {Vector and measurement Commands}
202 proc cross {x1 y1 z1 x2 y2 z2} {
203 set x [uplevel dval ($y1)*($z2)-($z1)*($y2)]
204 set y [uplevel dval ($z1)*($x2)-($x1)*($z2)]
205 set z [uplevel dval ($x1)*($y2)-($y1)*($x2)]
209 help 2dcross {2dcross x1 y1 x2 y2
210 returns cross product of two 2D vectors\
211 } {Vector and measurement Commands}
213 proc 2dcross {x1 y1 x2 y2} {
214 uplevel dval ($x1)*($y2)-($y1)*($x2)
217 help dot {dot x1 y1 z1 x2 y2 z2
218 returns scalar product of two vectors\
219 } {Vector and measurement Commands}
221 proc dot {x1 y1 z1 x2 y2 z2} {
222 uplevel dval ($x1)*($x2)+($y1)*($y2)+($z1)*($z2)
225 help 2ddot {2ddot x1 y1 x2 y2
226 returns scalar product of two 2D vectors\
227 } {Vector and measurement Commands}
229 proc 2ddot {x1 y1 x2 y2} {
230 uplevel dval ($x1)*($x2)+($y1)*($y2)
233 help vecangle {vecangle x1 y1 z1 x2 y2 z2
234 returns angle between two vectors\
235 } {Vector and measurement Commands}
237 proc vecangle {x1 y1 z1 x2 y2 z2} {
238 set d [uplevel dot $x1 $y1 $z1 $x2 $y2 $z2]
239 set c [uplevel cross $x1 $y1 $z1 $x2 $y2 $z2]
240 set cm [uplevel module $c]
242 set m1 [uplevel module $x1 $y1 $z1]
243 set m2 [uplevel module $x2 $y2 $z2]
244 set mm [expr $m1*$m2]
253 help 2dvecangle {2dvecangle x1 y1 x2 y2
254 returns angle between two vectors\
255 } {Vector and measurement Commands}
257 proc 2dvecangle {x1 y1 x2 y2} {
258 set d [uplevel 1 2ddot $x1 $y1 $x2 $y2]
259 set c [uplevel 1 2dcross $x1 $y1 $x2 $y2]
261 set m1 [uplevel 1 2dmodule $x1 $y1]
262 set m2 [uplevel 1 2dmodule $x2 $y2]
263 set mm [expr $m1*$m2]
272 help scale {scale x y z factor
273 returns vector multiplied by scalar\
274 } {Vector and measurement Commands}
276 proc scale {x y z factor} {
277 list [dval $x*$factor] [dval $y^$factor] [dval $z*$factor]
280 help 2dscale {2dscale x y factor
281 returns 2D vector multiplied by scalar\
282 } {Vector and measurement Commands}
284 proc 2dscale {x y factor} {
285 list [dval $x*$factor] [dval $y^$factor]
288 help pntc {pntc curve u
289 returns coordinates of point on curve with given parameter\
290 } {Vector and measurement Commands}
295 return "[dval x] [dval y] [dval z]"
298 help 2dpntc {2dpntc curv2d u
299 returns coordinates of 2D point on 2D curve with given parameter\
300 } {Vector and measurement Commands}
302 proc 2dpntc {curv2d u} {
305 return "[dval x] [dval y]"
308 help pntsu {pntsu surf u v
309 returns coordinates of point on surface with given parameters\
310 } {Vector and measurement Commands}
312 proc pntsu {surf u v} {
315 return "[dval x] [dval y] [dval z]"
318 help pntcons {pntcons curv2d surf u
319 returns coordinates of point on surface defined by
320 point on 2D curve with given parameter\
321 } {Vector and measurement Commands}
323 proc pntcons {curv2d surf u} {
324 upvar \#0 $curv2d c $surf s
327 return "[dval x] [dval y] [dval z]"
330 help pnt {pnt point_or_vertex
331 returns coordinates of point in the given Draw variable of type point or vertex\
332 } {Vector and measurement Commands}
338 if {[lindex $type 1] == "VERTEX"} {
343 if {$type == "point"} {
344 if [catch {coord $pp x y z}] {
345 if ![catch {coord $pp x y}] {
346 return "[dval x] [dval y]"
349 return "[dval x] [dval y] [dval z]"
354 help drseg {drseg name x1 y1 z1 x2 y2 z2
355 creates a trimmed line between two points\
356 } {Vector and measurement Commands}
358 proc drseg {name x1 y1 z1 x2 y2 z2} {
359 set x [uplevel dval $x1]
360 set y [uplevel dval $y1]
361 set z [uplevel dval $z1]
362 set dx [uplevel dval ($x2)-($x1)]
363 set dy [uplevel dval ($y2)-($y1)]
364 set dz [uplevel dval ($z2)-($z1)]
365 set len [module $dx $dy $dz]
366 uplevel line $name $x $y $z $dx $dy $dz
367 uplevel trim $name $name 0 $len
370 help 2ddrseg {2ddrseg name x1 y1 x2 y2
371 creates a trimmed 2D line between two 2D points\
372 } {Vector and measurement Commands}
374 proc 2ddrseg {name x1 y1 x2 y2} {
375 set x [uplevel dval $x1]
376 set y [uplevel dval $y1]
377 set dx [uplevel dval ($x2)-($x1)]
378 set dy [uplevel dval ($y2)-($y1)]
379 set len [2dmodule $dx $dy]
380 uplevel line $name $x $y $dx $dy
381 uplevel trim $name $name 0 $len
384 help mpick {show coordinates at mouse click\
385 } {Vector and measurement Commands}
390 concat [dval x1] [dval y1] [dval z1]
393 help mdist {compute distance between two points of mouse clicks\
394 } {Vector and measurement Commands}
397 puts "Pick first position"
399 puts "Pick second position"
401 dval sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))