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 scale {scale x y z factor
234 returns vector multiplied by scalar\
235 } {Vector and measurement Commands}
237 proc scale {x y z factor} {
238 list [dval $x*$factor] [dval $y^$factor] [dval $z*$factor]
241 help 2dscale {2dscale x y factor
242 returns 2D vector multiplied by scalar\
243 } {Vector and measurement Commands}
245 proc 2dscale {x y factor} {
246 list [dval $x*$factor] [dval $y^$factor]
249 help pntc {pntc curve u
250 returns coordinates of point on curve with given parameter\
251 } {Vector and measurement Commands}
256 return "[dval x] [dval y] [dval z]"
259 help 2dpntc {2dpntc curv2d u
260 returns coordinates of 2D point on 2D curve with given parameter\
261 } {Vector and measurement Commands}
263 proc 2dpntc {curv2d u} {
266 return "[dval x] [dval y]"
269 help pntsu {pntsu surf u v
270 returns coordinates of point on surface with given parameters\
271 } {Vector and measurement Commands}
273 proc pntsu {surf u v} {
276 return "[dval x] [dval y] [dval z]"
279 help pntcons {pntcons curv2d surf u
280 returns coordinates of point on surface defined by
281 point on 2D curve with given parameter\
282 } {Vector and measurement Commands}
284 proc pntcons {curv2d surf u} {
285 upvar \#0 $curv2d c $surf s
288 return "[dval x] [dval y] [dval z]"
291 help pnt {pnt point_or_vertex
292 returns coordinates of point in the given Draw variable of type point or vertex\
293 } {Vector and measurement Commands}
299 if {[lindex $type 1] == "VERTEX"} {
304 if {$type == "point"} {
305 if [catch {coord $pp x y z}] {
306 if ![catch {coord $pp x y}] {
307 return "[dval x] [dval y]"
310 return "[dval x] [dval y] [dval z]"
315 help drseg {drseg name x1 y1 z1 x2 y2 z2
316 creates a trimmed line between two points\
317 } {Vector and measurement Commands}
319 proc drseg {name x1 y1 z1 x2 y2 z2} {
320 set x [uplevel dval $x1]
321 set y [uplevel dval $y1]
322 set z [uplevel dval $z1]
323 set dx [uplevel dval ($x2)-($x1)]
324 set dy [uplevel dval ($y2)-($y1)]
325 set dz [uplevel dval ($z2)-($z1)]
326 set len [module $dx $dy $dz]
327 uplevel line $name $x $y $z $dx $dy $dz
328 uplevel trim $name $name 0 $len
331 help 2ddrseg {2ddrseg name x1 y1 x2 y2
332 creates a trimmed 2D line between two 2D points\
333 } {Vector and measurement Commands}
335 proc 2ddrseg {name x1 y1 x2 y2} {
336 set x [uplevel dval $x1]
337 set y [uplevel dval $y1]
338 set dx [uplevel dval ($x2)-($x1)]
339 set dy [uplevel dval ($y2)-($y1)]
340 set len [2dmodule $dx $dy]
341 uplevel line $name $x $y $dx $dy
342 uplevel trim $name $name 0 $len
345 help mpick {show coordinates at mouse click\
346 } {Vector and measurement Commands}
351 concat [dval x1] [dval y1] [dval z1]
354 help mdist {compute distance between two points of mouse clicks\
355 } {Vector and measurement Commands}
358 puts "Pick first position"
360 puts "Pick second position"
362 dval sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))