COMPAS 2.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (468) hide show
  1. compas/__init__.py +274 -0
  2. compas/__main__.py +31 -0
  3. compas/_iotools.py +80 -0
  4. compas/_os.py +746 -0
  5. compas/colors/__init__.py +14 -0
  6. compas/colors/cmcrameri/acton.txt +256 -0
  7. compas/colors/cmcrameri/actonS.txt +100 -0
  8. compas/colors/cmcrameri/bam.txt +256 -0
  9. compas/colors/cmcrameri/bamO.txt +256 -0
  10. compas/colors/cmcrameri/bamako.txt +256 -0
  11. compas/colors/cmcrameri/bamakoS.txt +100 -0
  12. compas/colors/cmcrameri/batlow.txt +256 -0
  13. compas/colors/cmcrameri/batlowK.txt +256 -0
  14. compas/colors/cmcrameri/batlowS.txt +100 -0
  15. compas/colors/cmcrameri/batlowW.txt +256 -0
  16. compas/colors/cmcrameri/berlin.txt +256 -0
  17. compas/colors/cmcrameri/bilbao.txt +256 -0
  18. compas/colors/cmcrameri/bilbaoS.txt +100 -0
  19. compas/colors/cmcrameri/broc.txt +256 -0
  20. compas/colors/cmcrameri/brocO.txt +256 -0
  21. compas/colors/cmcrameri/buda.txt +256 -0
  22. compas/colors/cmcrameri/budaS.txt +100 -0
  23. compas/colors/cmcrameri/bukavu.txt +256 -0
  24. compas/colors/cmcrameri/cork.txt +256 -0
  25. compas/colors/cmcrameri/corkO.txt +256 -0
  26. compas/colors/cmcrameri/davos.txt +256 -0
  27. compas/colors/cmcrameri/davosS.txt +100 -0
  28. compas/colors/cmcrameri/devon.txt +256 -0
  29. compas/colors/cmcrameri/devonS.txt +100 -0
  30. compas/colors/cmcrameri/fes.txt +256 -0
  31. compas/colors/cmcrameri/grayC.txt +256 -0
  32. compas/colors/cmcrameri/grayCS.txt +100 -0
  33. compas/colors/cmcrameri/hawaii.txt +256 -0
  34. compas/colors/cmcrameri/hawaiiS.txt +100 -0
  35. compas/colors/cmcrameri/imola.txt +256 -0
  36. compas/colors/cmcrameri/imolaS.txt +100 -0
  37. compas/colors/cmcrameri/lajolla.txt +256 -0
  38. compas/colors/cmcrameri/lajollaS.txt +100 -0
  39. compas/colors/cmcrameri/lapaz.txt +256 -0
  40. compas/colors/cmcrameri/lapazS.txt +100 -0
  41. compas/colors/cmcrameri/lisbon.txt +256 -0
  42. compas/colors/cmcrameri/nuuk.txt +256 -0
  43. compas/colors/cmcrameri/nuukS.txt +100 -0
  44. compas/colors/cmcrameri/oleron.txt +256 -0
  45. compas/colors/cmcrameri/oslo.txt +256 -0
  46. compas/colors/cmcrameri/osloS.txt +100 -0
  47. compas/colors/cmcrameri/roma.txt +256 -0
  48. compas/colors/cmcrameri/romaO.txt +256 -0
  49. compas/colors/cmcrameri/tofino.txt +256 -0
  50. compas/colors/cmcrameri/tokyo.txt +256 -0
  51. compas/colors/cmcrameri/tokyoS.txt +100 -0
  52. compas/colors/cmcrameri/turku.txt +256 -0
  53. compas/colors/cmcrameri/turkuS.txt +100 -0
  54. compas/colors/cmcrameri/vanimo.txt +256 -0
  55. compas/colors/cmcrameri/vik.txt +256 -0
  56. compas/colors/cmcrameri/vikO.txt +256 -0
  57. compas/colors/color.py +1118 -0
  58. compas/colors/colordict.py +103 -0
  59. compas/colors/colormap.py +306 -0
  60. compas/colors/html_colors.py +149 -0
  61. compas/colors/mpl_colormap.py +1064 -0
  62. compas/data/__init__.py +31 -0
  63. compas/data/coercion.py +71 -0
  64. compas/data/conftest.py +10 -0
  65. compas/data/data.py +342 -0
  66. compas/data/encoders.py +240 -0
  67. compas/data/exceptions.py +2 -0
  68. compas/data/json.py +240 -0
  69. compas/data/samples/boxes.obj +250 -0
  70. compas/data/samples/butt_model.obj +2829 -0
  71. compas/data/samples/cone.stl +0 -0
  72. compas/data/samples/cube.off +16 -0
  73. compas/data/samples/cube_ascii.stl +56114 -0
  74. compas/data/samples/cube_binary.stl +0 -0
  75. compas/data/samples/cube_binary_2.stl +0 -0
  76. compas/data/samples/doosabin.json +1 -0
  77. compas/data/samples/faces.obj +63 -0
  78. compas/data/samples/faces_big.obj +9663 -0
  79. compas/data/samples/faces_reversed.obj +63 -0
  80. compas/data/samples/fink.obj +135 -0
  81. compas/data/samples/grid_irregular.obj +303 -0
  82. compas/data/samples/hypar.obj +148 -0
  83. compas/data/samples/lines.json +1 -0
  84. compas/data/samples/lines.obj +282 -0
  85. compas/data/samples/lines_big.obj +40322 -0
  86. compas/data/samples/lines_bigger.obj +161282 -0
  87. compas/data/samples/lines_noleaves.obj +422 -0
  88. compas/data/samples/mesh.obj +874 -0
  89. compas/data/samples/mesh_100_faces.json +1893 -0
  90. compas/data/samples/mesh_110_faces.json +2081 -0
  91. compas/data/samples/open_edges.obj +424 -0
  92. compas/data/samples/point.json +1 -0
  93. compas/data/samples/quadmesh.obj +183 -0
  94. compas/data/samples/quadmesh_planar.obj +181 -0
  95. compas/data/samples/saddle.obj +314 -0
  96. compas/data/samples/spline.obj +72 -0
  97. compas/data/samples/tubemesh.json +1 -0
  98. compas/data/samples/tubemesh.obj +378 -0
  99. compas/data/samples/tubemesh.off +374 -0
  100. compas/data/samples/tubemesh.ply +551 -0
  101. compas/data/schema.py +137 -0
  102. compas/data/validators.py +243 -0
  103. compas/datastructures/__init__.py +75 -0
  104. compas/datastructures/_mutablemapping.py +148 -0
  105. compas/datastructures/assembly/__init__.py +0 -0
  106. compas/datastructures/assembly/assembly.py +234 -0
  107. compas/datastructures/assembly/exceptions.py +6 -0
  108. compas/datastructures/assembly/part.py +228 -0
  109. compas/datastructures/attributes.py +85 -0
  110. compas/datastructures/cell_network/__init__.py +0 -0
  111. compas/datastructures/cell_network/cell_network.py +4190 -0
  112. compas/datastructures/datastructure.py +309 -0
  113. compas/datastructures/graph/__init__.py +0 -0
  114. compas/datastructures/graph/duality.py +197 -0
  115. compas/datastructures/graph/graph.py +2478 -0
  116. compas/datastructures/graph/operations/__init__.py +0 -0
  117. compas/datastructures/graph/operations/join.py +124 -0
  118. compas/datastructures/graph/operations/split.py +63 -0
  119. compas/datastructures/graph/planarity.py +292 -0
  120. compas/datastructures/graph/smoothing.py +59 -0
  121. compas/datastructures/mesh/__init__.py +0 -0
  122. compas/datastructures/mesh/conway.py +553 -0
  123. compas/datastructures/mesh/duality.py +104 -0
  124. compas/datastructures/mesh/mesh.py +5103 -0
  125. compas/datastructures/mesh/operations/__init__.py +0 -0
  126. compas/datastructures/mesh/operations/collapse.py +350 -0
  127. compas/datastructures/mesh/operations/extrude.py +0 -0
  128. compas/datastructures/mesh/operations/insert.py +120 -0
  129. compas/datastructures/mesh/operations/merge.py +83 -0
  130. compas/datastructures/mesh/operations/split.py +263 -0
  131. compas/datastructures/mesh/operations/substitute.py +37 -0
  132. compas/datastructures/mesh/operations/swap.py +62 -0
  133. compas/datastructures/mesh/operations/weld.py +107 -0
  134. compas/datastructures/mesh/remesh.py +234 -0
  135. compas/datastructures/mesh/slice.py +170 -0
  136. compas/datastructures/mesh/smoothing.py +187 -0
  137. compas/datastructures/mesh/subdivision.py +754 -0
  138. compas/datastructures/tree/__init__.py +0 -0
  139. compas/datastructures/tree/tree.py +504 -0
  140. compas/datastructures/volmesh/__init__.py +0 -0
  141. compas/datastructures/volmesh/volmesh.py +4203 -0
  142. compas/files/__init__.py +27 -0
  143. compas/files/_xml/__init__.py +0 -0
  144. compas/files/_xml/xml_cli.py +213 -0
  145. compas/files/_xml/xml_cpython.py +32 -0
  146. compas/files/_xml/xml_pre_38.py +58 -0
  147. compas/files/_xml/xml_shared.py +40 -0
  148. compas/files/gltf/__init__.py +0 -0
  149. compas/files/gltf/constants.py +59 -0
  150. compas/files/gltf/data_classes.py +657 -0
  151. compas/files/gltf/extensions.py +317 -0
  152. compas/files/gltf/gltf.py +92 -0
  153. compas/files/gltf/gltf_children.py +64 -0
  154. compas/files/gltf/gltf_content.py +615 -0
  155. compas/files/gltf/gltf_exporter.py +465 -0
  156. compas/files/gltf/gltf_mesh.py +273 -0
  157. compas/files/gltf/gltf_node.py +348 -0
  158. compas/files/gltf/gltf_parser.py +115 -0
  159. compas/files/gltf/gltf_reader.py +310 -0
  160. compas/files/gltf/gltf_scene.py +125 -0
  161. compas/files/gltf/helpers.py +59 -0
  162. compas/files/obj.py +680 -0
  163. compas/files/off.py +296 -0
  164. compas/files/ply.py +697 -0
  165. compas/files/stl.py +436 -0
  166. compas/files/xml.py +328 -0
  167. compas/geometry/__init__.py +772 -0
  168. compas/geometry/_core/__init__.py +0 -0
  169. compas/geometry/_core/_algebra.py +2826 -0
  170. compas/geometry/_core/angles.py +408 -0
  171. compas/geometry/_core/centroids.py +573 -0
  172. compas/geometry/_core/distance.py +895 -0
  173. compas/geometry/_core/normals.py +149 -0
  174. compas/geometry/_core/nurbs.py +367 -0
  175. compas/geometry/_core/predicates_2.py +582 -0
  176. compas/geometry/_core/predicates_3.py +1111 -0
  177. compas/geometry/_core/quaternions.py +196 -0
  178. compas/geometry/_core/size.py +204 -0
  179. compas/geometry/_core/tangent.py +56 -0
  180. compas/geometry/_core/transformations.py +1121 -0
  181. compas/geometry/_core/transformations_numpy.py +279 -0
  182. compas/geometry/bbox.py +145 -0
  183. compas/geometry/bbox_numpy.py +306 -0
  184. compas/geometry/bestfit.py +77 -0
  185. compas/geometry/bestfit_numpy.py +259 -0
  186. compas/geometry/booleans.py +297 -0
  187. compas/geometry/booleans_shapely.py +147 -0
  188. compas/geometry/brep/__init__.py +112 -0
  189. compas/geometry/brep/brep.py +1131 -0
  190. compas/geometry/brep/edge.py +302 -0
  191. compas/geometry/brep/errors.py +22 -0
  192. compas/geometry/brep/face.py +329 -0
  193. compas/geometry/brep/loop.py +87 -0
  194. compas/geometry/brep/trim.py +46 -0
  195. compas/geometry/brep/vertex.py +46 -0
  196. compas/geometry/contours.py +32 -0
  197. compas/geometry/curves/__init__.py +0 -0
  198. compas/geometry/curves/arc.py +429 -0
  199. compas/geometry/curves/bezier.py +345 -0
  200. compas/geometry/curves/circle.py +500 -0
  201. compas/geometry/curves/conic.py +16 -0
  202. compas/geometry/curves/curve.py +614 -0
  203. compas/geometry/curves/ellipse.py +408 -0
  204. compas/geometry/curves/hyperbola.py +336 -0
  205. compas/geometry/curves/line.py +431 -0
  206. compas/geometry/curves/nurbs.py +412 -0
  207. compas/geometry/curves/parabola.py +256 -0
  208. compas/geometry/curves/polyline.py +724 -0
  209. compas/geometry/frame.py +769 -0
  210. compas/geometry/geometry.py +296 -0
  211. compas/geometry/hull.py +146 -0
  212. compas/geometry/hull_numpy.py +82 -0
  213. compas/geometry/icp_numpy.py +104 -0
  214. compas/geometry/interpolation_barycentric.py +38 -0
  215. compas/geometry/interpolation_coons.py +102 -0
  216. compas/geometry/interpolation_tweening.py +87 -0
  217. compas/geometry/intersection.py +121 -0
  218. compas/geometry/intersections.py +1105 -0
  219. compas/geometry/kdtree.py +153 -0
  220. compas/geometry/offset.py +208 -0
  221. compas/geometry/pca_numpy.py +87 -0
  222. compas/geometry/plane.py +657 -0
  223. compas/geometry/point.py +664 -0
  224. compas/geometry/pointcloud.py +409 -0
  225. compas/geometry/polygon.py +473 -0
  226. compas/geometry/polyhedron.py +650 -0
  227. compas/geometry/projection.py +155 -0
  228. compas/geometry/quadmesh_planarize.py +30 -0
  229. compas/geometry/quadmesh_planarize_none.py +114 -0
  230. compas/geometry/quaternion.py +544 -0
  231. compas/geometry/reflection.py +102 -0
  232. compas/geometry/rotation.py +354 -0
  233. compas/geometry/scale.py +100 -0
  234. compas/geometry/shapes/__init__.py +0 -0
  235. compas/geometry/shapes/box.py +747 -0
  236. compas/geometry/shapes/capsule.py +472 -0
  237. compas/geometry/shapes/cone.py +309 -0
  238. compas/geometry/shapes/cylinder.py +355 -0
  239. compas/geometry/shapes/shape.py +300 -0
  240. compas/geometry/shapes/sphere.py +362 -0
  241. compas/geometry/shapes/torus.py +294 -0
  242. compas/geometry/shear.py +115 -0
  243. compas/geometry/surfaces/__init__.py +0 -0
  244. compas/geometry/surfaces/conical.py +166 -0
  245. compas/geometry/surfaces/cylindrical.py +251 -0
  246. compas/geometry/surfaces/extrusion.py +0 -0
  247. compas/geometry/surfaces/nurbs.py +380 -0
  248. compas/geometry/surfaces/planar.py +223 -0
  249. compas/geometry/surfaces/revolution.py +0 -0
  250. compas/geometry/surfaces/spherical.py +259 -0
  251. compas/geometry/surfaces/surface.py +689 -0
  252. compas/geometry/surfaces/toroidal.py +198 -0
  253. compas/geometry/transformation.py +554 -0
  254. compas/geometry/translation.py +104 -0
  255. compas/geometry/triangulation_delaunay.py +102 -0
  256. compas/geometry/triangulation_earclip.py +300 -0
  257. compas/geometry/trimesh_curvature.py +113 -0
  258. compas/geometry/trimesh_descent_numpy.py +26 -0
  259. compas/geometry/trimesh_geodistance.py +35 -0
  260. compas/geometry/trimesh_gradient_numpy.py +74 -0
  261. compas/geometry/trimesh_isolines.py +41 -0
  262. compas/geometry/trimesh_matrices.py +31 -0
  263. compas/geometry/trimesh_matrices_numpy.py +191 -0
  264. compas/geometry/trimesh_parametrisation.py +55 -0
  265. compas/geometry/trimesh_pull_points_numpy.py +216 -0
  266. compas/geometry/trimesh_remeshing.py +97 -0
  267. compas/geometry/trimesh_samplepoints_numpy.py +108 -0
  268. compas/geometry/trimesh_slicing.py +28 -0
  269. compas/geometry/trimesh_smoothing_numpy.py +34 -0
  270. compas/geometry/vector.py +889 -0
  271. compas/itertools.py +441 -0
  272. compas/linalg.py +634 -0
  273. compas/matrices.py +314 -0
  274. compas/plugins.py +488 -0
  275. compas/rpc/__init__.py +20 -0
  276. compas/rpc/__main__.py +77 -0
  277. compas/rpc/dispatcher.py +181 -0
  278. compas/rpc/errors.py +11 -0
  279. compas/rpc/proxy.py +437 -0
  280. compas/rpc/server.py +79 -0
  281. compas/rpc/services/__init__.py +0 -0
  282. compas/rpc/services/default.py +87 -0
  283. compas/rpc/services/watcher.py +43 -0
  284. compas/scene/__init__.py +56 -0
  285. compas/scene/assemblyobject.py +3 -0
  286. compas/scene/context.py +154 -0
  287. compas/scene/descriptors/__init__.py +0 -0
  288. compas/scene/descriptors/attribute.py +30 -0
  289. compas/scene/descriptors/color.py +91 -0
  290. compas/scene/descriptors/colordict.py +101 -0
  291. compas/scene/descriptors/protocol.py +12 -0
  292. compas/scene/exceptions.py +7 -0
  293. compas/scene/geometryobject.py +58 -0
  294. compas/scene/graphobject.py +173 -0
  295. compas/scene/meshobject.py +238 -0
  296. compas/scene/scene.py +144 -0
  297. compas/scene/sceneobject.py +253 -0
  298. compas/scene/volmeshobject.py +252 -0
  299. compas/tolerance.py +879 -0
  300. compas/topology/__init__.py +45 -0
  301. compas/topology/combinatorics.py +108 -0
  302. compas/topology/connectivity.py +129 -0
  303. compas/topology/orientation.py +204 -0
  304. compas/topology/traversal.py +567 -0
  305. compas/utilities/__init__.py +25 -0
  306. compas/utilities/azync.py +155 -0
  307. compas/utilities/datetime.py +40 -0
  308. compas/utilities/decorators.py +136 -0
  309. compas/utilities/remote.py +54 -0
  310. compas/utilities/ssh.py +169 -0
  311. compas-2.1.1.dist-info/AUTHORS.md +42 -0
  312. compas-2.1.1.dist-info/LICENSE +21 -0
  313. compas-2.1.1.dist-info/METADATA +132 -0
  314. compas-2.1.1.dist-info/RECORD +468 -0
  315. compas-2.1.1.dist-info/WHEEL +5 -0
  316. compas-2.1.1.dist-info/entry_points.txt +2 -0
  317. compas-2.1.1.dist-info/top_level.txt +4 -0
  318. compas_blender/__init__.py +250 -0
  319. compas_blender/collections.py +130 -0
  320. compas_blender/conversions/__init__.py +48 -0
  321. compas_blender/conversions/breps.py +0 -0
  322. compas_blender/conversions/colors.py +23 -0
  323. compas_blender/conversions/curves.py +125 -0
  324. compas_blender/conversions/geometry.py +161 -0
  325. compas_blender/conversions/meshes.py +148 -0
  326. compas_blender/conversions/surfaces.py +48 -0
  327. compas_blender/conversions/transformations.py +46 -0
  328. compas_blender/data.py +30 -0
  329. compas_blender/geometry/__init__.py +14 -0
  330. compas_blender/geometry/booleans.py +100 -0
  331. compas_blender/install.py +338 -0
  332. compas_blender/objects.py +326 -0
  333. compas_blender/print_python_path.py +18 -0
  334. compas_blender/scene/__init__.py +116 -0
  335. compas_blender/scene/boxobject.py +64 -0
  336. compas_blender/scene/capsuleobject.py +73 -0
  337. compas_blender/scene/circleobject.py +53 -0
  338. compas_blender/scene/coneobject.py +70 -0
  339. compas_blender/scene/curveobject.py +58 -0
  340. compas_blender/scene/cylinderobject.py +70 -0
  341. compas_blender/scene/frameobject.py +105 -0
  342. compas_blender/scene/graphobject.py +257 -0
  343. compas_blender/scene/lineobject.py +60 -0
  344. compas_blender/scene/meshobject.py +508 -0
  345. compas_blender/scene/planeobject.py +81 -0
  346. compas_blender/scene/pointcloudobject.py +67 -0
  347. compas_blender/scene/pointobject.py +71 -0
  348. compas_blender/scene/polygonobject.py +57 -0
  349. compas_blender/scene/polyhedronobject.py +57 -0
  350. compas_blender/scene/polylineobject.py +54 -0
  351. compas_blender/scene/sceneobject.py +249 -0
  352. compas_blender/scene/sphereobject.py +72 -0
  353. compas_blender/scene/surfaceobject.py +54 -0
  354. compas_blender/scene/torusobject.py +73 -0
  355. compas_blender/scene/vectorobject.py +69 -0
  356. compas_blender/scene/volmeshobject.py +475 -0
  357. compas_blender/utilities/__init__.py +38 -0
  358. compas_blender/utilities/drawing.py +768 -0
  359. compas_ghpython/__init__.py +124 -0
  360. compas_ghpython/components/Compas_FromJson/code.py +18 -0
  361. compas_ghpython/components/Compas_Info/code.py +15 -0
  362. compas_ghpython/components/Compas_RpcCall/code.py +31 -0
  363. compas_ghpython/components/Compas_ToJson/code.py +19 -0
  364. compas_ghpython/components/Compas_ToRhinoGeometry/code.py +15 -0
  365. compas_ghpython/components/__init__.py +99 -0
  366. compas_ghpython/install.py +29 -0
  367. compas_ghpython/scene/__init__.py +115 -0
  368. compas_ghpython/scene/boxobject.py +40 -0
  369. compas_ghpython/scene/brepobject.py +40 -0
  370. compas_ghpython/scene/capsuleobject.py +41 -0
  371. compas_ghpython/scene/circleobject.py +40 -0
  372. compas_ghpython/scene/coneobject.py +40 -0
  373. compas_ghpython/scene/curveobject.py +42 -0
  374. compas_ghpython/scene/cylinderobject.py +38 -0
  375. compas_ghpython/scene/ellipseobject.py +40 -0
  376. compas_ghpython/scene/frameobject.py +67 -0
  377. compas_ghpython/scene/graphobject.py +77 -0
  378. compas_ghpython/scene/lineobject.py +39 -0
  379. compas_ghpython/scene/meshobject.py +137 -0
  380. compas_ghpython/scene/planeobject.py +61 -0
  381. compas_ghpython/scene/pointobject.py +38 -0
  382. compas_ghpython/scene/polygonobject.py +48 -0
  383. compas_ghpython/scene/polyhedronobject.py +48 -0
  384. compas_ghpython/scene/polylineobject.py +39 -0
  385. compas_ghpython/scene/sceneobject.py +12 -0
  386. compas_ghpython/scene/sphereobject.py +39 -0
  387. compas_ghpython/scene/surfaceobject.py +42 -0
  388. compas_ghpython/scene/torusobject.py +39 -0
  389. compas_ghpython/scene/vectorobject.py +51 -0
  390. compas_ghpython/scene/volmeshobject.py +154 -0
  391. compas_ghpython/sets.py +83 -0
  392. compas_ghpython/timer.py +36 -0
  393. compas_ghpython/uninstall.py +31 -0
  394. compas_ghpython/utilities/__init__.py +33 -0
  395. compas_ghpython/utilities/drawing.py +457 -0
  396. compas_rhino/__init__.py +607 -0
  397. compas_rhino/conduits/__init__.py +16 -0
  398. compas_rhino/conduits/base.py +121 -0
  399. compas_rhino/conduits/faces.py +91 -0
  400. compas_rhino/conduits/labels.py +102 -0
  401. compas_rhino/conduits/lines.py +109 -0
  402. compas_rhino/conduits/points.py +111 -0
  403. compas_rhino/conversions/__init__.py +168 -0
  404. compas_rhino/conversions/breps.py +122 -0
  405. compas_rhino/conversions/curves.py +453 -0
  406. compas_rhino/conversions/docobjects.py +0 -0
  407. compas_rhino/conversions/exceptions.py +7 -0
  408. compas_rhino/conversions/extrusions.py +80 -0
  409. compas_rhino/conversions/geometry.py +187 -0
  410. compas_rhino/conversions/meshes.py +281 -0
  411. compas_rhino/conversions/shapes.py +291 -0
  412. compas_rhino/conversions/surfaces.py +355 -0
  413. compas_rhino/conversions/transformations.py +45 -0
  414. compas_rhino/geometry/__init__.py +44 -0
  415. compas_rhino/geometry/booleans.py +118 -0
  416. compas_rhino/geometry/brep/__init__.py +40 -0
  417. compas_rhino/geometry/brep/brep.py +554 -0
  418. compas_rhino/geometry/brep/builder.py +184 -0
  419. compas_rhino/geometry/brep/edge.py +202 -0
  420. compas_rhino/geometry/brep/face.py +285 -0
  421. compas_rhino/geometry/brep/loop.py +129 -0
  422. compas_rhino/geometry/brep/trim.py +99 -0
  423. compas_rhino/geometry/brep/vertex.py +76 -0
  424. compas_rhino/geometry/curves/__init__.py +43 -0
  425. compas_rhino/geometry/curves/curve.py +382 -0
  426. compas_rhino/geometry/curves/nurbs.py +298 -0
  427. compas_rhino/geometry/surfaces/__init__.py +50 -0
  428. compas_rhino/geometry/surfaces/nurbs.py +370 -0
  429. compas_rhino/geometry/surfaces/surface.py +407 -0
  430. compas_rhino/geometry/trimesh_curvature.py +344 -0
  431. compas_rhino/geometry/trimesh_slicing.py +65 -0
  432. compas_rhino/install.py +382 -0
  433. compas_rhino/install_plugin.py +169 -0
  434. compas_rhino/install_with_pip.py +20 -0
  435. compas_rhino/layers.py +347 -0
  436. compas_rhino/objects.py +1141 -0
  437. compas_rhino/print_python_path.py +18 -0
  438. compas_rhino/scene/__init__.py +123 -0
  439. compas_rhino/scene/boxobject.py +44 -0
  440. compas_rhino/scene/brepobject.py +41 -0
  441. compas_rhino/scene/capsuleobject.py +45 -0
  442. compas_rhino/scene/circleobject.py +43 -0
  443. compas_rhino/scene/coneobject.py +43 -0
  444. compas_rhino/scene/curveobject.py +43 -0
  445. compas_rhino/scene/cylinderobject.py +43 -0
  446. compas_rhino/scene/ellipseobject.py +44 -0
  447. compas_rhino/scene/frameobject.py +99 -0
  448. compas_rhino/scene/graphobject.py +364 -0
  449. compas_rhino/scene/helpers.py +33 -0
  450. compas_rhino/scene/lineobject.py +43 -0
  451. compas_rhino/scene/meshobject.py +673 -0
  452. compas_rhino/scene/planeobject.py +71 -0
  453. compas_rhino/scene/pointobject.py +43 -0
  454. compas_rhino/scene/polygonobject.py +45 -0
  455. compas_rhino/scene/polyhedronobject.py +45 -0
  456. compas_rhino/scene/polylineobject.py +43 -0
  457. compas_rhino/scene/sceneobject.py +123 -0
  458. compas_rhino/scene/sphereobject.py +43 -0
  459. compas_rhino/scene/surfaceobject.py +42 -0
  460. compas_rhino/scene/torusobject.py +43 -0
  461. compas_rhino/scene/vectorobject.py +49 -0
  462. compas_rhino/scene/volmeshobject.py +518 -0
  463. compas_rhino/ui.py +114 -0
  464. compas_rhino/uninstall.py +243 -0
  465. compas_rhino/uninstall_plugin.py +91 -0
  466. compas_rhino/utilities/__init__.py +37 -0
  467. compas_rhino/utilities/constructors.py +71 -0
  468. compas_rhino/utilities/drawing.py +1081 -0
@@ -0,0 +1,2826 @@
1
+ from __future__ import absolute_import
2
+ from __future__ import division
3
+ from __future__ import print_function
4
+
5
+ from copy import deepcopy
6
+ from math import acos
7
+ from math import asin
8
+ from math import atan2
9
+ from math import cos
10
+ from math import fabs
11
+ from math import pi
12
+ from math import sin
13
+ from math import sqrt
14
+ from math import tan
15
+
16
+ from compas.tolerance import TOL
17
+
18
+ _SPEC2TUPLE = {
19
+ "sxyz": (0, 0, 0, 0),
20
+ "sxyx": (0, 0, 1, 0),
21
+ "sxzy": (0, 1, 0, 0),
22
+ "sxzx": (0, 1, 1, 0),
23
+ "syzx": (1, 0, 0, 0),
24
+ "syzy": (1, 0, 1, 0),
25
+ "syxz": (1, 1, 0, 0),
26
+ "syxy": (1, 1, 1, 0),
27
+ "szxy": (2, 0, 0, 0),
28
+ "szxz": (2, 0, 1, 0),
29
+ "szyx": (2, 1, 0, 0),
30
+ "szyz": (2, 1, 1, 0),
31
+ "rzyx": (0, 0, 0, 1),
32
+ "rxyx": (0, 0, 1, 1),
33
+ "ryzx": (0, 1, 0, 1),
34
+ "rxzx": (0, 1, 1, 1),
35
+ "rxzy": (1, 0, 0, 1),
36
+ "ryzy": (1, 0, 1, 1),
37
+ "rzxy": (1, 1, 0, 1),
38
+ "ryxy": (1, 1, 1, 1),
39
+ "ryxz": (2, 0, 0, 1),
40
+ "rzxz": (2, 0, 1, 1),
41
+ "rxyz": (2, 1, 0, 1),
42
+ "rzyz": (2, 1, 1, 1),
43
+ }
44
+ """used for Euler angles: to map rotation type and axes to tuples of inner axis, parity, repetition, frame"""
45
+
46
+ _NEXT_SPEC = [1, 2, 0, 1]
47
+
48
+
49
+ def vector_average(vector):
50
+ """Average of a vector.
51
+
52
+ Parameters
53
+ ----------
54
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
55
+ List of values.
56
+
57
+ Returns
58
+ -------
59
+ float
60
+ The mean value.
61
+ """
62
+ return sum(vector) / float(len(vector))
63
+
64
+
65
+ def vector_variance(vector):
66
+ """Variance of a vector.
67
+
68
+ Parameters
69
+ ----------
70
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
71
+ List of values.
72
+
73
+ Returns
74
+ -------
75
+ float
76
+ The variance value.
77
+ """
78
+ m = vector_average(vector)
79
+ return (sum([(i - m) ** 2 for i in vector]) / float(len(vector))) ** 0.5
80
+
81
+
82
+ def vector_standard_deviation(vector):
83
+ """Standard deviation of a vector.
84
+
85
+ Parameters
86
+ ----------
87
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
88
+ List of values.
89
+
90
+ Returns
91
+ -------
92
+ float
93
+ The standard deviation value.
94
+ """
95
+ return vector_variance(vector) ** 0.5
96
+
97
+
98
+ def argmax(values):
99
+ """Returns the index of the first maximum value within an array.
100
+
101
+ Parameters
102
+ ----------
103
+ values : sequence[float]
104
+ A list of values.
105
+
106
+ Returns
107
+ -------
108
+ int
109
+ The index of the first maximum value within an array.
110
+
111
+ Notes
112
+ -----
113
+ NumPy's *argmax* function [1]_ is different, it returns an array of indices.
114
+
115
+ References
116
+ ----------
117
+ .. [1] https://numpy.org/doc/stable/reference/generated/numpy.argmax.html
118
+
119
+ Examples
120
+ --------
121
+ >>> argmax([2, 4, 4, 3])
122
+ 1
123
+
124
+ """
125
+ return max(range(len(values)), key=lambda i: values[i]) # type: ignore
126
+
127
+
128
+ def argmin(values):
129
+ """Returns the index of the first minimum value within an array.
130
+
131
+ Parameters
132
+ ----------
133
+ values : sequence[float]
134
+ A list of values.
135
+
136
+ Returns
137
+ -------
138
+ int
139
+ The index of the first minimum value within an array.
140
+
141
+ Notes
142
+ -----
143
+ NumPy's *argmin* function [1]_ is different, it returns an array of indices.
144
+
145
+ References
146
+ ----------
147
+ .. [1] https://numpy.org/doc/stable/reference/generated/numpy.argmin.html
148
+
149
+ Examples
150
+ --------
151
+ >>> argmin([4, 2, 2, 3])
152
+ 1
153
+
154
+ """
155
+ return min(range(len(values)), key=lambda i: values[i])
156
+
157
+
158
+ # ==============================================================================
159
+ # these return something of smaller dimension/length/...
160
+ # something_(of)vector/s
161
+ # ==============================================================================
162
+
163
+
164
+ def sum_vectors(vectors, axis=0):
165
+ """Calculate the sum of a series of vectors along the specified axis.
166
+
167
+ Parameters
168
+ ----------
169
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
170
+ A list of vectors.
171
+ axis : int, optional
172
+ If ``axis == 0``, the sum is taken per column.
173
+ If ``axis == 1``, the sum is taken per row.
174
+
175
+ Returns
176
+ -------
177
+ list[float]
178
+ The length of the list is ``len(vectors[0])``, if ``axis == 0``.
179
+ The length is ``len(vectors)``, otherwise.
180
+
181
+ Examples
182
+ --------
183
+ >>> vectors = [[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]
184
+ >>> sum_vectors(vectors)
185
+ [3.0, 6.0, 9.0]
186
+ >>> sum_vectors(vectors, axis=1)
187
+ [6.0, 6.0, 6.0]
188
+
189
+ """
190
+ if axis == 0:
191
+ vectors = zip(*vectors)
192
+ return [sum(vector) for vector in vectors]
193
+
194
+
195
+ def norm_vector(vector):
196
+ """Calculate the length of a vector.
197
+
198
+ Parameters
199
+ ----------
200
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
201
+ XYZ components of the vector.
202
+
203
+ Returns
204
+ -------
205
+ float
206
+ The L2 norm, or *length* of the vector.
207
+
208
+ Examples
209
+ --------
210
+ >>> norm_vector([2.0, 0.0, 0.0])
211
+ 2.0
212
+
213
+ >>> norm_vector([1.0, 1.0, 0.0]) == sqrt(2.0)
214
+ True
215
+
216
+ """
217
+ return sqrt(sum(axis**2 for axis in vector))
218
+
219
+
220
+ def norm_vectors(vectors):
221
+ """
222
+ Calculate the norm of each vector in a list of vectors.
223
+
224
+ Parameters
225
+ ----------
226
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
227
+ A list of vectors
228
+
229
+ Returns
230
+ -------
231
+ list[float]
232
+ A list with the lengths of all vectors.
233
+
234
+ Examples
235
+ --------
236
+ >>> norm_vectors([[1.0, 0.0, 0.0], [2.0, 0.0, 0.0], [3.0, 0.0, 0.0]])
237
+ [1.0, 2.0, 3.0]
238
+
239
+ """
240
+ return [norm_vector(vector) for vector in vectors]
241
+
242
+
243
+ def length_vector(vector):
244
+ """Calculate the length of the vector.
245
+
246
+ Parameters
247
+ ----------
248
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
249
+ XYZ components of the vector.
250
+
251
+ Returns
252
+ -------
253
+ float
254
+ The length of the vector.
255
+
256
+ Examples
257
+ --------
258
+ >>> length_vector([2.0, 0.0, 0.0])
259
+ 2.0
260
+
261
+ >>> length_vector([1.0, 1.0, 0.0]) == sqrt(2.0)
262
+ True
263
+
264
+ """
265
+ return sqrt(length_vector_sqrd(vector))
266
+
267
+
268
+ def length_vector_xy(vector):
269
+ """Compute the length of a vector, assuming it lies in the XY plane.
270
+
271
+ Parameters
272
+ ----------
273
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
274
+ XY(Z) components of the vector.
275
+
276
+ Returns
277
+ -------
278
+ float
279
+ The length of the XY component of the vector.
280
+
281
+ Examples
282
+ --------
283
+ >>> length_vector_xy([2.0, 0.0])
284
+ 2.0
285
+
286
+ >>> length_vector_xy([2.0, 0.0, 0.0])
287
+ 2.0
288
+
289
+ >>> length_vector_xy([2.0, 0.0, 2.0])
290
+ 2.0
291
+
292
+ """
293
+ return sqrt(length_vector_sqrd_xy(vector))
294
+
295
+
296
+ def length_vector_sqrd(vector):
297
+ """Compute the squared length of a vector.
298
+
299
+ Parameters
300
+ ----------
301
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
302
+ XYZ components of the vector.
303
+
304
+ Returns
305
+ -------
306
+ float
307
+ The squared length.
308
+
309
+ Examples
310
+ --------
311
+ >>> length_vector_sqrd([1.0, 1.0, 0.0])
312
+ 2.0
313
+
314
+ """
315
+ return vector[0] ** 2 + vector[1] ** 2 + vector[2] ** 2
316
+
317
+
318
+ def length_vector_sqrd_xy(vector):
319
+ """Compute the squared length of a vector, assuming it lies in the XY plane.
320
+
321
+ Parameters
322
+ ----------
323
+ vector : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
324
+ XY(Z) components of the vector.
325
+
326
+ Returns
327
+ -------
328
+ float
329
+ The squared length.
330
+
331
+ Examples
332
+ --------
333
+ >>> length_vector_sqrd_xy([1.0, 1.0])
334
+ 2.0
335
+
336
+ >>> length_vector_sqrd_xy([1.0, 1.0, 0.0])
337
+ 2.0
338
+
339
+ >>> length_vector_sqrd_xy([1.0, 1.0, 1.0])
340
+ 2.0
341
+
342
+ """
343
+ return vector[0] ** 2 + vector[1] ** 2
344
+
345
+
346
+ # ==============================================================================
347
+ # these perform an operation on a vector and return a modified vector
348
+ # -> elementwise operations on 1 vector
349
+ # should this not bet ...ed_vector
350
+ # ... or else modify the vector in-place
351
+ # ==============================================================================
352
+
353
+
354
+ def scale_vector(vector, factor):
355
+ """Scale a vector by a given factor.
356
+
357
+ Parameters
358
+ ----------
359
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
360
+ XYZ components of the vector.
361
+ factor : float
362
+ The scaling factor.
363
+
364
+ Returns
365
+ -------
366
+ [float, float, float]
367
+ The scaled vector.
368
+
369
+ Examples
370
+ --------
371
+ >>> scale_vector([1.0, 2.0, 3.0], 2.0)
372
+ [2.0, 4.0, 6.0]
373
+
374
+ >>> v = [2.0, 0.0, 0.0]
375
+ >>> scale_vector(v, 1 / length_vector(v))
376
+ [1.0, 0.0, 0.0]
377
+
378
+ """
379
+ return [axis * factor for axis in vector]
380
+
381
+
382
+ def scale_vector_xy(vector, factor):
383
+ """Scale a vector by a given factor, assuming it lies in the XY plane.
384
+
385
+ Parameters
386
+ ----------
387
+ vector : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
388
+ XY(Z) components of the vector.
389
+ scale : float
390
+ Scale factor.
391
+
392
+ Returns
393
+ -------
394
+ [float, float, 0.0]
395
+ The scaled vector in the XY-plane.
396
+
397
+ Examples
398
+ --------
399
+ >>> scale_vector_xy([1.0, 2.0, 3.0], 2.0)
400
+ [2.0, 4.0, 0.0]
401
+
402
+ """
403
+ return [vector[0] * factor, vector[1] * factor, 0.0]
404
+
405
+
406
+ def scale_vectors(vectors, factor):
407
+ """Scale multiple vectors by a given factor.
408
+
409
+ Parameters
410
+ ----------
411
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
412
+ A list of vectors.
413
+ factor : float
414
+ The scaling factor.
415
+
416
+ Returns
417
+ -------
418
+ list[[float, float, float]]
419
+ The scaled vectors.
420
+
421
+ Examples
422
+ --------
423
+ >>>
424
+
425
+ """
426
+ return [scale_vector(vector, factor) for vector in vectors]
427
+
428
+
429
+ def scale_vectors_xy(vectors, factor):
430
+ """Scale multiple vectors by a given factor, assuming they lie in the XY plane.
431
+
432
+ Parameters
433
+ ----------
434
+ vectors : sequence[[float, float] or [float, float, float] | :class:`compas.geometry.Vector`]
435
+ A list of vectors.
436
+ factor : float
437
+ The scaling factor.
438
+
439
+ Returns
440
+ -------
441
+ list[[float, float, 0.0]]
442
+ The scaled vectors in the XY plane.
443
+
444
+ Examples
445
+ --------
446
+ >>>
447
+
448
+ """
449
+ return [scale_vector_xy(vector, factor) for vector in vectors]
450
+
451
+
452
+ def normalize_vector(vector):
453
+ """Normalise a given vector.
454
+
455
+ Parameters
456
+ ----------
457
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
458
+ XYZ components of the vector.
459
+
460
+ Returns
461
+ -------
462
+ [float, float, float]
463
+ The normalized vector.
464
+
465
+ Examples
466
+ --------
467
+ >>>
468
+
469
+ """
470
+ length = length_vector(vector)
471
+ if not length:
472
+ return vector
473
+ return [vector[0] / length, vector[1] / length, vector[2] / length]
474
+
475
+
476
+ def normalize_vector_xy(vector):
477
+ """Normalize a vector, assuming it lies in the XY-plane.
478
+
479
+ Parameters
480
+ ----------
481
+ vector : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
482
+ XY(Z) components of the vector.
483
+
484
+ Returns
485
+ -------
486
+ [float, float, 0.0]
487
+ The normalized vector in the XY-plane.
488
+
489
+ Examples
490
+ --------
491
+ >>>
492
+
493
+ """
494
+ length = length_vector_xy(vector)
495
+ if not length:
496
+ return vector
497
+ return [vector[0] / length, vector[1] / length, 0.0]
498
+
499
+
500
+ def normalize_vectors(vectors):
501
+ """Normalise multiple vectors.
502
+
503
+ Parameters
504
+ ----------
505
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
506
+ A list of vectors.
507
+
508
+ Returns
509
+ -------
510
+ list[[float, float, float]]
511
+ The normalized vectors.
512
+
513
+ Examples
514
+ --------
515
+ >>>
516
+
517
+ """
518
+ return [normalize_vector(vector) for vector in vectors]
519
+
520
+
521
+ def normalize_vectors_xy(vectors):
522
+ """Normalise multiple vectors, assuming they lie in the XY plane.
523
+
524
+ Parameters
525
+ ----------
526
+ vectors : sequence[[float, float] or [float, float, float] | :class:`compas.geometry.Vector`]
527
+ A list of vectors.
528
+
529
+ Returns
530
+ -------
531
+ list[[float, float, 0.0]]
532
+ The normalized vectors in the XY plane.
533
+
534
+ Examples
535
+ --------
536
+ >>>
537
+
538
+ """
539
+ return [normalize_vector_xy(vector) for vector in vectors]
540
+
541
+
542
+ def power_vector(vector, power):
543
+ """Raise a vector to the given power.
544
+
545
+ Parameters
546
+ ----------
547
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
548
+ XYZ components of the vector.
549
+ power : int, float
550
+ The power to which to raise the vector.
551
+
552
+ Returns
553
+ -------
554
+ [float, float, float]
555
+ The raised vector.
556
+
557
+ Examples
558
+ --------
559
+ >>>
560
+
561
+ """
562
+ return [axis**power for axis in vector]
563
+
564
+
565
+ def power_vectors(vectors, power):
566
+ """Raise a list of vectors to the given power.
567
+
568
+ Parameters
569
+ ----------
570
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
571
+ A list of vectors.
572
+ power : int, float
573
+ The power to which to raise the vectors.
574
+
575
+ Returns
576
+ -------
577
+ list[[float, float, float]]
578
+ The raised vectors.
579
+
580
+ Examples
581
+ --------
582
+ >>>
583
+
584
+ """
585
+ return [power_vector(vector, power) for vector in vectors]
586
+
587
+
588
+ def square_vector(vector):
589
+ """Raise a vector to the power 2.
590
+
591
+ Parameters
592
+ ----------
593
+ vector : [float, float, float] | :class:`compas.geometry.Vector`
594
+ XYZ components of the vector.
595
+
596
+ Returns
597
+ -------
598
+ [float, float, float]
599
+ The squared vector.
600
+
601
+ Examples
602
+ --------
603
+ >>>
604
+
605
+ """
606
+ return power_vector(vector, 2)
607
+
608
+
609
+ def square_vectors(vectors):
610
+ """Raise a multiple vectors to the power 2.
611
+
612
+ Parameters
613
+ ----------
614
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
615
+ A list of vectors.
616
+
617
+ Returns
618
+ -------
619
+ [float, float, float]]
620
+ The squared vectors.
621
+
622
+ Examples
623
+ --------
624
+ >>>
625
+
626
+ """
627
+ return [square_vectors(vector) for vector in vectors]
628
+
629
+
630
+ # ==============================================================================
631
+ # these perform an operation with corresponding elements of the (2) input vectors as operands
632
+ # and return a vector with the results
633
+ # -> elementwise operations on two vectors
634
+ # ==============================================================================
635
+
636
+
637
+ def add_vectors(u, v):
638
+ """Add two vectors.
639
+
640
+ Parameters
641
+ ----------
642
+ u : [float, float, float] | :class:`compas.geometry.Vector`
643
+ XYZ components of the first vector.
644
+ v : [float, float, float] | :class:`compas.geometry.Vector`
645
+ XYZ components of the second vector.
646
+
647
+ Returns
648
+ -------
649
+ [float, float, float]
650
+ The resulting vector.
651
+
652
+ """
653
+ return [a + b for (a, b) in zip(u, v)]
654
+
655
+
656
+ def add_vectors_xy(u, v):
657
+ """Add two vectors, assuming they lie in the XY-plane.
658
+
659
+ Parameters
660
+ ----------
661
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
662
+ XY(Z) components of the first vector.
663
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
664
+ XY(Z) components of the second vector.
665
+
666
+ Returns
667
+ -------
668
+ [float, float, 0.0]
669
+ Resulting vector in the XY-plane.
670
+
671
+ Examples
672
+ --------
673
+ >>>
674
+
675
+ """
676
+ return [u[0] + v[0], u[1] + v[1], 0.0]
677
+
678
+
679
+ def subtract_vectors(u, v):
680
+ """Subtract one vector from another.
681
+
682
+ Parameters
683
+ ----------
684
+ u : [float, float, float] | :class:`compas.geometry.Vector`
685
+ XYZ components of the first vector.
686
+ v : [float, float, float] | :class:`compas.geometry.Vector`
687
+ XYZ components of the second vector.
688
+
689
+ Returns
690
+ -------
691
+ [float, float, float]
692
+ The resulting vector.
693
+
694
+ Examples
695
+ --------
696
+ >>>
697
+
698
+ """
699
+ return [a - b for (a, b) in zip(u, v)]
700
+
701
+
702
+ def subtract_vectors_xy(u, v):
703
+ """Subtract one vector from another, assuming they lie in the XY plane.
704
+
705
+ Parameters
706
+ ----------
707
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
708
+ The XY(Z) components of the first vector.
709
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
710
+ The XY(Z) components of the second vector.
711
+
712
+ Returns
713
+ -------
714
+ [float, float, 0.0]
715
+ Resulting vector in the XY-plane.
716
+
717
+ Examples
718
+ --------
719
+ >>>
720
+
721
+ """
722
+ return [u[0] - v[0], u[1] - v[1], 0.0]
723
+
724
+
725
+ def multiply_vectors(u, v):
726
+ """Element-wise multiplication of two vectors.
727
+
728
+ Parameters
729
+ ----------
730
+ u : [float, float, float] | :class:`compas.geometry.Vector`
731
+ The XYZ components of the first vector.
732
+ v : l[float, float, float] | :class:`compas.geometry.Vector`
733
+ The XYZ components of the second vector.
734
+
735
+ Returns
736
+ -------
737
+ [float, float, float]
738
+ Resulting vector.
739
+
740
+ Examples
741
+ --------
742
+ >>>
743
+
744
+ """
745
+ return [a * b for (a, b) in zip(u, v)]
746
+
747
+
748
+ def multiply_vectors_xy(u, v):
749
+ """Element-wise multiplication of two vectors assumed to lie in the XY plane.
750
+
751
+ Parameters
752
+ ----------
753
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
754
+ The XY(Z) components of the first vector.
755
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
756
+ The XY(Z) components of the second vector.
757
+
758
+ Returns
759
+ -------
760
+ [float, float, 0.0]
761
+ Resulting vector in the XY plane.
762
+
763
+ Examples
764
+ --------
765
+ >>>
766
+
767
+ """
768
+ return [u[0] * v[0], u[1] * v[1], 0.0]
769
+
770
+
771
+ def divide_vectors(u, v):
772
+ """Element-wise division of two vectors.
773
+
774
+ Parameters
775
+ ----------
776
+ u : [float, float, float] | :class:`compas.geometry.Vector`
777
+ The XYZ components of the first vector.
778
+ v : [float, float, float] | :class:`compas.geometry.Vector`
779
+ The XYZ components of the second vector.
780
+
781
+ Returns
782
+ -------
783
+ [float, float, float]
784
+ Resulting vector.
785
+
786
+ Examples
787
+ --------
788
+ >>>
789
+
790
+ """
791
+ return [a / b for (a, b) in zip(u, v)]
792
+
793
+
794
+ def divide_vectors_xy(u, v):
795
+ """Element-wise division of two vectors assumed to lie in the XY plane.
796
+
797
+ Parameters
798
+ ----------
799
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
800
+ The XY(Z) components of the first vector.
801
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
802
+ The XY(Z) components of the second vector.
803
+
804
+ Returns
805
+ -------
806
+ [float, float, 0.0]
807
+ Resulting vector in the XY plane.
808
+
809
+ Examples
810
+ --------
811
+ >>>
812
+
813
+ """
814
+ return [u[0] / v[0], u[1] / v[1], 0.0]
815
+
816
+
817
+ # ==============================================================================
818
+ # ...
819
+ # ==============================================================================
820
+
821
+
822
+ def cross_vectors(u, v):
823
+ r"""Compute the cross product of two vectors.
824
+
825
+ Parameters
826
+ ----------
827
+ u : [float, float, float] | :class:`compas.geometry.Vector`
828
+ XYZ components of the first vector.
829
+ v : [float, float, float] | :class:`compas.geometry.Vector`
830
+ XYZ components of the second vector.
831
+
832
+ Returns
833
+ -------
834
+ [float, float, float]
835
+ The cross product of the two vectors.
836
+
837
+ Notes
838
+ -----
839
+ The xyz components of the cross product of two vectors :math:`\mathbf{u}`
840
+ and :math:`\mathbf{v}` can be computed as the *minors* of the following matrix:
841
+
842
+ .. math::
843
+ :nowrap:
844
+
845
+ \begin{bmatrix}
846
+ x & y & z \\
847
+ u_{x} & u_{y} & u_{z} \\
848
+ v_{x} & v_{y} & v_{z}
849
+ \end{bmatrix}
850
+
851
+ Therefore, the cross product can be written as:
852
+
853
+ .. math::
854
+ :nowrap:
855
+
856
+ \begin{eqnarray}
857
+ \mathbf{u} \times \mathbf{v}
858
+ & =
859
+ \begin{bmatrix}
860
+ u_{y} * v_{z} - u_{z} * v_{y} \\
861
+ u_{z} * v_{x} - u_{x} * v_{z} \\
862
+ u_{x} * v_{y} - u_{y} * v_{x}
863
+ \end{bmatrix}
864
+ \end{eqnarray}
865
+
866
+ Examples
867
+ --------
868
+ >>> cross_vectors([1.0, 0.0, 0.0], [0.0, 1.0, 0.0])
869
+ [0.0, 0.0, 1.0]
870
+
871
+ """
872
+ return [
873
+ u[1] * v[2] - u[2] * v[1],
874
+ u[2] * v[0] - u[0] * v[2],
875
+ u[0] * v[1] - u[1] * v[0],
876
+ ]
877
+
878
+
879
+ def cross_vectors_xy(u, v):
880
+ """Compute the cross product of two vectors, assuming they lie in the XY-plane.
881
+
882
+ Parameters
883
+ ----------
884
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
885
+ XY(Z) coordinates of the first vector.
886
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
887
+ XY(Z) coordinates of the second vector.
888
+
889
+ Returns
890
+ -------
891
+ [float, float, float]
892
+ The cross product of the two vectors.
893
+ This vector will be perpendicular to the XY plane.
894
+
895
+ Examples
896
+ --------
897
+ >>> cross_vectors_xy([1.0, 0.0], [0.0, 1.0])
898
+ [0.0, 0.0, 1.0]
899
+
900
+ >>> cross_vectors_xy([1.0, 0.0, 0.0], [0.0, 1.0, 0.0])
901
+ [0.0, 0.0, 1.0]
902
+
903
+ >>> cross_vectors_xy([1.0, 0.0, 1.0], [0.0, 1.0, 1.0])
904
+ [0.0, 0.0, 1.0]
905
+
906
+ """
907
+ return [0.0, 0.0, u[0] * v[1] - u[1] * v[0]]
908
+
909
+
910
+ def dot_vectors(u, v):
911
+ """Compute the dot product of two vectors.
912
+
913
+ Parameters
914
+ ----------
915
+ u : [float, float, float] | :class:`compas.geometry.Vector`
916
+ XYZ components of the first vector.
917
+ v : [float, float, float] | :class:`compas.geometry.Vector`
918
+ XYZ components of the second vector.
919
+
920
+ Returns
921
+ -------
922
+ float
923
+ The dot product of the two vectors.
924
+
925
+ Examples
926
+ --------
927
+ >>> dot_vectors([1.0, 0, 0], [2.0, 0, 0])
928
+ 2.0
929
+
930
+ """
931
+ return sum(a * b for a, b in zip(u, v))
932
+
933
+
934
+ def dot_vectors_xy(u, v):
935
+ """Compute the dot product of two vectors, assuming they lie in the XY-plane.
936
+
937
+ Parameters
938
+ ----------
939
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
940
+ XY(Z) coordinates of the first vector.
941
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
942
+ XY(Z) coordinates of the second vector.
943
+
944
+ Returns
945
+ -------
946
+ float
947
+ The dot product of the XY components of the two vectors.
948
+
949
+ Examples
950
+ --------
951
+ >>> dot_vectors_xy([1.0, 0], [2.0, 0])
952
+ 2.0
953
+
954
+ >>> dot_vectors_xy([1.0, 0, 0], [2.0, 0, 0])
955
+ 2.0
956
+
957
+ >>> dot_vectors_xy([1.0, 0, 1], [2.0, 0, 1])
958
+ 2.0
959
+
960
+ """
961
+ return u[0] * v[0] + u[1] * v[1]
962
+
963
+
964
+ def vector_component(u, v):
965
+ """Compute the component of u in the direction of v.
966
+
967
+ Parameters
968
+ ----------
969
+ u : [float, float, float] | :class:`compas.geometry.Vector`
970
+ XYZ components of the vector.
971
+ v : [float, float, float] | :class:`compas.geometry.Vector`
972
+ XYZ components of the direction.
973
+
974
+ Returns
975
+ -------
976
+ [float, float, float]
977
+ The component of u in the direction of v.
978
+
979
+ Notes
980
+ -----
981
+ This is similar to computing direction cosines, or to the projection of
982
+ a vector onto another vector. See the respective Wikipedia pages ([1]_, [2]_)
983
+ for more info.
984
+
985
+ References
986
+ ----------
987
+ .. [1] *Direction cosine*. Available at https://en.wikipedia.org/wiki/Direction_cosine.
988
+ .. [2] *Vector projection*. Available at https://en.wikipedia.org/wiki/Vector_projection.
989
+
990
+ Examples
991
+ --------
992
+ >>> vector_component([1.0, 2.0, 3.0], [1.0, 0.0, 0.0])
993
+ [1.0, 0.0, 0.0]
994
+
995
+ """
996
+ l2 = length_vector_sqrd(v)
997
+ if not l2:
998
+ return [0, 0, 0]
999
+ x = dot_vectors(u, v) / l2
1000
+ return scale_vector(v, x)
1001
+
1002
+
1003
+ def vector_component_xy(u, v):
1004
+ """Compute the component of u in the direction of v, assuming they lie in the XY-plane.
1005
+
1006
+ Parameters
1007
+ ----------
1008
+ u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
1009
+ XYZ components of the vector.
1010
+ v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
1011
+ XYZ components of the direction.
1012
+
1013
+ Returns
1014
+ -------
1015
+ [float, float, 0.0]
1016
+ The component of u in the XY plane, in the direction of v.
1017
+
1018
+ Notes
1019
+ -----
1020
+ This is similar to computing direction cosines, or to the projection of
1021
+ a vector onto another vector. See the respective Wikipedia pages ([1]_, [2]_)
1022
+ for more info.
1023
+
1024
+ References
1025
+ ----------
1026
+ .. [1] *Direction cosine*. Available at https://en.wikipedia.org/wiki/Direction_cosine.
1027
+ .. [2] *Vector projection*. Available at https://en.wikipedia.org/wiki/Vector_projection.
1028
+
1029
+ Examples
1030
+ --------
1031
+ >>> vector_component_xy([1, 2, 0], [1, 0, 0])
1032
+ [1.0, 0.0, 0.0]
1033
+
1034
+ """
1035
+ l2 = length_vector_sqrd_xy(v)
1036
+ if not l2:
1037
+ return [0, 0, 0]
1038
+ x = dot_vectors_xy(u, v) / l2
1039
+ return scale_vector_xy(v, x)
1040
+
1041
+
1042
+ # ==============================================================================
1043
+ # linalg
1044
+ # ==============================================================================
1045
+
1046
+
1047
+ def homogenize_vectors(vectors, w=1.0):
1048
+ """Homogenise a list of vectors.
1049
+
1050
+ Parameters
1051
+ ----------
1052
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
1053
+ A list of vectors.
1054
+ w : float, optional
1055
+ Homogenisation parameter.
1056
+
1057
+ Returns
1058
+ -------
1059
+ list[[float, float, float]]
1060
+ Homogenised vectors.
1061
+
1062
+ Notes
1063
+ -----
1064
+ Vectors described by XYZ components are homogenised by appending a homogenisation
1065
+ parameter to the components, and by dividing each component by that parameter.
1066
+ Homogenisatioon of vectors is often used in relation to transformations.
1067
+
1068
+ Examples
1069
+ --------
1070
+ >>> vectors = [[1.0, 0.0, 0.0]]
1071
+ >>> homogenize_vectors(vectors)
1072
+ [[1.0, 0.0, 0.0, 1.0]]
1073
+
1074
+ """
1075
+ return [[x / w, y / w, z / w, w] for x, y, z in vectors]
1076
+
1077
+
1078
+ def dehomogenize_vectors(vectors):
1079
+ """Dehomogenise a list of vectors.
1080
+
1081
+ Parameters
1082
+ ----------
1083
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
1084
+ A list of vectors.
1085
+
1086
+ Returns
1087
+ -------
1088
+ list[float, float, float]
1089
+ Dehomogenised vectors.
1090
+
1091
+ Examples
1092
+ --------
1093
+ >>>
1094
+
1095
+ """
1096
+ return [[x * w, y * w, z * w] for x, y, z, w in vectors]
1097
+
1098
+
1099
+ def orthonormalize_vectors(vectors):
1100
+ """Orthonormalize a set of vectors.
1101
+
1102
+ Parameters
1103
+ ----------
1104
+ vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
1105
+ The set of vectors to othonormalize.
1106
+
1107
+ Returns
1108
+ -------
1109
+ list[[float, float, float]]
1110
+ An othonormal basis for the input vectors.
1111
+
1112
+ Notes
1113
+ -----
1114
+ This creates a basis for the range (column space) of the matrix A.T,
1115
+ with A = vectors.
1116
+
1117
+ Orthonormalisation is according to the Gram-Schmidt process.
1118
+
1119
+ Examples
1120
+ --------
1121
+ >>> orthonormalize_vectors([[1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
1122
+ [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
1123
+
1124
+ """
1125
+ basis = []
1126
+ for v in vectors:
1127
+ if basis:
1128
+ e = subtract_vectors(v, sum_vectors([vector_component(v, b) for b in basis]))
1129
+ else:
1130
+ e = v
1131
+ if any(axis > 1e-10 for axis in e):
1132
+ basis.append(normalize_vector(e))
1133
+ return basis
1134
+
1135
+
1136
+ # =============================================================================
1137
+ # general matrices
1138
+ # =============================================================================
1139
+
1140
+
1141
+ def transpose_matrix(M):
1142
+ """Transpose a matrix.
1143
+
1144
+ Parameters
1145
+ ----------
1146
+ M : list[list[float]] | :class:`compas.geometry.Transformation`
1147
+ The matrix to be transposed.
1148
+
1149
+ Returns
1150
+ -------
1151
+ list[list[float]]
1152
+ The result matrix.
1153
+
1154
+ """
1155
+ return list(map(list, zip(*list(M))))
1156
+
1157
+
1158
+ def multiply_matrices(A, B):
1159
+ r"""Mutliply a matrix with a matrix.
1160
+
1161
+ Parameters
1162
+ ----------
1163
+ A : list[list[float]] | :class:`compas.geometry.Transformation`
1164
+ The first matrix.
1165
+ B : list[list[float]] | :class:`compas.geometry.Transformation`
1166
+ The second matrix.
1167
+
1168
+ Returns
1169
+ -------
1170
+ list[list[float]]
1171
+ The result matrix.
1172
+
1173
+ Raises
1174
+ ------
1175
+ Exception
1176
+ If the shapes of the matrices are not compatible.
1177
+ If the row length of B is inconsistent.
1178
+
1179
+ Notes
1180
+ -----
1181
+ This is a pure Python version of the following linear algebra procedure:
1182
+
1183
+ .. math::
1184
+
1185
+ \mathbf{A} \cdot \mathbf{B} = \mathbf{C}
1186
+
1187
+ with :math:`\mathbf{A}` [m x n], :math:`\mathbf{B}` [n x o], and :math:`\mathbf{C}` [m x o].
1188
+
1189
+ Examples
1190
+ --------
1191
+ >>> A = [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]]
1192
+ >>> B = [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]]
1193
+ >>> multiply_matrices(A, B)
1194
+ [[4.0, 0.0, 0.0], [0.0, 4.0, 0.0], [0.0, 0.0, 4.0]]
1195
+
1196
+ """
1197
+ A = list(A)
1198
+ B = list(B)
1199
+ n = len(B) # number of rows in B
1200
+ o = len(B[0]) # number of cols in B
1201
+ if not all(len(row) == o for row in B):
1202
+ raise Exception("Row length in matrix B is inconsistent.")
1203
+ if not all([len(row) == n for row in A]):
1204
+ raise Exception("Matrix shapes are not compatible.")
1205
+ B = list(zip(*list(B)))
1206
+ return [[dot_vectors(row, col) for col in B] for row in A]
1207
+
1208
+
1209
+ def multiply_matrix_vector(A, b):
1210
+ r"""Multiply a matrix with a vector.
1211
+
1212
+ Parameters
1213
+ ----------
1214
+ A : list[list[float]] | :class:`compas.geometry.Transformation`
1215
+ The matrix.
1216
+ b : [float, float, float] | :class:`compas.geometry.Vector`
1217
+ The vector.
1218
+
1219
+ Returns
1220
+ -------
1221
+ [float, float, float]
1222
+ The resulting vector.
1223
+
1224
+ Raises
1225
+ ------
1226
+ Exception
1227
+ If not all rows of the matrix have the same length as the vector.
1228
+
1229
+ Notes
1230
+ -----
1231
+ This is a Python version of the following linear algebra procedure:
1232
+
1233
+ .. math::
1234
+
1235
+ \mathbf{A} \cdot \mathbf{x} = \mathbf{b}
1236
+
1237
+ with :math:`\mathbf{A}` a *m* by *n* matrix, :math:`\mathbf{x}` a vector of
1238
+ length *n*, and :math:`\mathbf{b}` a vector of length *m*.
1239
+
1240
+ Examples
1241
+ --------
1242
+ >>> matrix = [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]]
1243
+ >>> vector = [1.0, 2.0, 3.0]
1244
+ >>> multiply_matrix_vector(matrix, vector)
1245
+ [2.0, 4.0, 6.0]
1246
+
1247
+ """
1248
+ n = len(b)
1249
+ if not all([len(row) == n for row in A]):
1250
+ raise Exception("Matrix shape is not compatible with vector length.")
1251
+ return [dot_vectors(row, b) for row in A]
1252
+
1253
+
1254
+ def is_matrix_square(M):
1255
+ """Verify that a matrix is square.
1256
+
1257
+ Parameters
1258
+ ----------
1259
+ M : list[list[float]]
1260
+ The matrix.
1261
+
1262
+ Returns
1263
+ -------
1264
+ bool
1265
+ True if the length of every row is equal to the number of rows.
1266
+ False otherwise.
1267
+
1268
+ See Also
1269
+ --------
1270
+ is_matrix_symmetric
1271
+
1272
+ Examples
1273
+ --------
1274
+ >>> M = identity_matrix(4)
1275
+ >>> is_matrix_square(M)
1276
+ True
1277
+
1278
+ """
1279
+ number_of_rows = len(M)
1280
+ for row in M:
1281
+ if len(row) != number_of_rows:
1282
+ return False
1283
+ return True
1284
+
1285
+
1286
+ def matrix_minor(M, i, j):
1287
+ """Construct the minor corresponding to an element of a matrix.
1288
+
1289
+ Parameters
1290
+ ----------
1291
+ M : list[list[float]]
1292
+ The matrix.
1293
+ i : int
1294
+ Row index of the minor.
1295
+ j : int
1296
+ Column index of the minor.
1297
+
1298
+ Returns
1299
+ -------
1300
+ list[list[float]]
1301
+ The minor.
1302
+
1303
+ See Also
1304
+ --------
1305
+ matrix_determinant
1306
+ matrix_inverse
1307
+
1308
+ """
1309
+ return [row[:j] + row[j + 1 :] for row in (M[:i] + M[i + 1 :])]
1310
+
1311
+
1312
+ def matrix_determinant(M, check=True):
1313
+ """Calculates the determinant of a square matrix M.
1314
+
1315
+ Parameters
1316
+ ----------
1317
+ M : list[list[float]]
1318
+ A square matrix of any dimension.
1319
+ check : bool
1320
+ If True, checks if the matrix is square.
1321
+
1322
+ Raises
1323
+ ------
1324
+ ValueError
1325
+ If the matrix is not square.
1326
+
1327
+ Returns
1328
+ -------
1329
+ float
1330
+ The determinant.
1331
+
1332
+ See Also
1333
+ --------
1334
+ matrix_minor
1335
+ matrix_inverse
1336
+
1337
+ Examples
1338
+ --------
1339
+ >>> M = identity_matrix(4)
1340
+ >>> matrix_determinant(M)
1341
+ 1.0
1342
+
1343
+ """
1344
+ dim = len(M)
1345
+
1346
+ if check:
1347
+ if not is_matrix_square(M):
1348
+ raise ValueError("Not a square matrix")
1349
+
1350
+ if dim == 2:
1351
+ return M[0][0] * M[1][1] - M[0][1] * M[1][0]
1352
+
1353
+ D = 0
1354
+ for c in range(dim):
1355
+ D += (-1) ** c * M[0][c] * matrix_determinant(matrix_minor(M, 0, c), check=False)
1356
+ return D
1357
+
1358
+
1359
+ def matrix_inverse(M):
1360
+ """Calculates the inverse of a square matrix M.
1361
+
1362
+ Parameters
1363
+ ----------
1364
+ M : list[list[float]]
1365
+ A square matrix of any dimension.
1366
+
1367
+ Returns
1368
+ -------
1369
+ list[list[float]]
1370
+ The inverted matrix.
1371
+
1372
+ Raises
1373
+ ------
1374
+ ValueError
1375
+ If the matrix is not squared
1376
+ ValueError
1377
+ If the matrix is singular.
1378
+ ValueError
1379
+ If the matrix is not invertible.
1380
+
1381
+ See Also
1382
+ --------
1383
+ matrix_minor
1384
+ matrix_determinant
1385
+
1386
+ Examples
1387
+ --------
1388
+ >>> from compas.geometry import Frame
1389
+ >>> f = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
1390
+ >>> T = matrix_from_frame(f)
1391
+ >>> I = multiply_matrices(T, matrix_inverse(T))
1392
+ >>> I2 = identity_matrix(4)
1393
+ >>> allclose(I[0], I2[0])
1394
+ True
1395
+ >>> allclose(I[1], I2[1])
1396
+ True
1397
+ >>> allclose(I[2], I2[2])
1398
+ True
1399
+ >>> allclose(I[3], I2[3])
1400
+ True
1401
+
1402
+ """
1403
+ D = matrix_determinant(M)
1404
+
1405
+ if D == 0:
1406
+ ValueError("The matrix is singular.")
1407
+
1408
+ if len(M) == 2:
1409
+ return [[M[1][1] / D, -1 * M[0][1] / D], [-1 * M[1][0] / D, M[0][0] / D]]
1410
+
1411
+ cofactors = []
1412
+ for r in range(len(M)):
1413
+ cofactor_row = []
1414
+ for c in range(len(M)):
1415
+ cofactor_row.append((-1) ** (r + c) * matrix_determinant(matrix_minor(M, r, c)))
1416
+ cofactors.append(cofactor_row)
1417
+
1418
+ cofactors = transpose_matrix(cofactors)
1419
+
1420
+ for r in range(len(cofactors)):
1421
+ for c in range(len(cofactors)):
1422
+ cofactors[r][c] = cofactors[r][c] / D
1423
+
1424
+ return cofactors
1425
+
1426
+
1427
+ # =============================================================================
1428
+ # 4x4 matrices
1429
+ # =============================================================================
1430
+
1431
+
1432
+ def decompose_matrix(M):
1433
+ """Calculates the components of rotation, translation, scale, shear, and
1434
+ perspective of a given transformation matrix M. [1]_
1435
+
1436
+ Parameters
1437
+ ----------
1438
+ M : list[list[float]]
1439
+ The square matrix of any dimension.
1440
+
1441
+ Raises
1442
+ ------
1443
+ ValueError
1444
+ If matrix is singular or degenerative.
1445
+
1446
+ Returns
1447
+ -------
1448
+ scale : [float, float, float]
1449
+ The 3 scale factors in x-, y-, and z-direction.
1450
+ shear : [float, float, float]
1451
+ The 3 shear factors for x-y, x-z, and y-z axes.
1452
+ angles : [float, float, float]
1453
+ The rotation specified through the 3 Euler angles about static x, y, z axes.
1454
+ translation : [float, float, float]
1455
+ The 3 values of translation.
1456
+ perspective : [float, float, float, float]
1457
+ The 4 perspective entries of the matrix.
1458
+
1459
+ See Also
1460
+ --------
1461
+ compose_matrix
1462
+
1463
+ Examples
1464
+ --------
1465
+ >>> trans1 = [1, 2, 3]
1466
+ >>> angle1 = [-2.142, 1.141, -0.142]
1467
+ >>> scale1 = [0.123, 2, 0.5]
1468
+ >>> T = matrix_from_translation(trans1)
1469
+ >>> R = matrix_from_euler_angles(angle1)
1470
+ >>> S = matrix_from_scale_factors(scale1)
1471
+ >>> M = multiply_matrices(multiply_matrices(T, R), S)
1472
+ >>> # M = compose_matrix(scale1, None, angle1, trans1, None)
1473
+ >>> scale2, shear2, angle2, trans2, persp2 = decompose_matrix(M)
1474
+ >>> allclose(scale1, scale2)
1475
+ True
1476
+ >>> allclose(angle1, angle2)
1477
+ True
1478
+ >>> allclose(trans1, trans2)
1479
+ True
1480
+
1481
+ References
1482
+ ----------
1483
+ .. [1] Slabaugh, 1999. *Computing Euler angles from a rotation matrix*.
1484
+ Available at: http://www.gregslabaugh.net/publications/euler.pdf
1485
+
1486
+ """
1487
+ detM = matrix_determinant(M) # raises ValueError if matrix is not squared
1488
+ if detM == 0:
1489
+ ValueError("The matrix is singular.")
1490
+
1491
+ Mt = transpose_matrix(M)
1492
+ if TOL.is_zero(Mt[3][3]):
1493
+ raise ValueError("The element [3,3] of the matrix is zero.")
1494
+
1495
+ for i in range(4):
1496
+ for j in range(4):
1497
+ Mt[i][j] /= Mt[3][3]
1498
+
1499
+ translation = [M[0][3], M[1][3], M[2][3]]
1500
+
1501
+ # scale, shear, angles
1502
+ scale = [0.0, 0.0, 0.0]
1503
+ shear = [0.0, 0.0, 0.0]
1504
+ angles = [0.0, 0.0, 0.0]
1505
+
1506
+ # copy Mt[:3, :3] into row
1507
+ row = [
1508
+ [0, 0, 0],
1509
+ [0, 0, 0],
1510
+ [0, 0, 0],
1511
+ ]
1512
+ for i in range(3):
1513
+ for j in range(3):
1514
+ row[i][j] = Mt[i][j]
1515
+
1516
+ scale[0] = norm_vector(row[0])
1517
+ for i in range(3):
1518
+ row[0][i] /= scale[0] # type: ignore
1519
+
1520
+ shear[0] = dot_vectors(row[0], row[1])
1521
+ for i in range(3):
1522
+ row[1][i] -= row[0][i] * shear[0]
1523
+
1524
+ scale[1] = norm_vector(row[1])
1525
+ for i in range(3):
1526
+ row[1][i] /= scale[1] # type: ignore
1527
+
1528
+ shear[1] = dot_vectors(row[0], row[2])
1529
+ for i in range(3):
1530
+ row[2][i] -= row[0][i] * shear[1]
1531
+
1532
+ # why is the order different here?
1533
+ # it certainly influences the result
1534
+
1535
+ shear[2] = dot_vectors(row[1], row[2])
1536
+ for i in range(3):
1537
+ row[2][i] -= row[0][i] * shear[2]
1538
+
1539
+ scale[2] = norm_vector(row[2])
1540
+ for i in range(3):
1541
+ row[2][i] /= scale[2] # type: ignore
1542
+
1543
+ shear[0] /= scale[1]
1544
+ shear[1] /= scale[2]
1545
+ shear[2] /= scale[2]
1546
+
1547
+ if dot_vectors(row[0], cross_vectors(row[1], row[2])) < 0:
1548
+ scale = [-x for x in scale]
1549
+ row = [[-x for x in y] for y in row]
1550
+
1551
+ # angles
1552
+ if row[0][2] != -1.0 and row[0][2] != 1.0:
1553
+ beta1 = asin(-row[0][2])
1554
+ # beta2 = pi - beta1
1555
+ alpha1 = atan2(row[1][2] / cos(beta1), row[2][2] / cos(beta1))
1556
+ # alpha2 = atan2(row[1][2] / cos(beta2), row[2][2] / cos(beta2))
1557
+ gamma1 = atan2(row[0][1] / cos(beta1), row[0][0] / cos(beta1))
1558
+ # gamma2 = atan2(row[0][1] / cos(beta2), row[0][0] / cos(beta2))
1559
+ angles = [alpha1, beta1, gamma1]
1560
+
1561
+ else:
1562
+ gamma = 0.0
1563
+ if row[0][2] == -1.0:
1564
+ beta = pi / 2.0
1565
+ alpha = gamma + atan2(row[1][0], row[2][0])
1566
+ else: # row[0][2] == 1
1567
+ beta = -pi / 2.0
1568
+ alpha = -gamma + atan2(-row[1][0], -row[2][0])
1569
+ angles = [alpha, beta, gamma]
1570
+
1571
+ # perspective
1572
+ if not TOL.is_zero(Mt[0][3]) and not TOL.is_zero(Mt[1][3]) and not TOL.is_zero(Mt[2][3]):
1573
+ P = deepcopy(Mt)
1574
+ P[0][3], P[1][3], P[2][3], P[3][3] = 0.0, 0.0, 0.0, 1.0
1575
+ Ptinv = matrix_inverse(transpose_matrix(P))
1576
+ perspective = multiply_matrix_vector(Ptinv, [Mt[0][3], Mt[1][3], Mt[2][3], Mt[3][3]])
1577
+ else:
1578
+ perspective = [0.0, 0.0, 0.0, 1.0]
1579
+
1580
+ return scale, shear, angles, translation, perspective
1581
+
1582
+
1583
+ def compose_matrix(scale=None, shear=None, angles=None, translation=None, perspective=None):
1584
+ """Calculates a matrix from the components of scale, shear, euler_angles, translation and perspective.
1585
+
1586
+ Parameters
1587
+ ----------
1588
+ scale : [float, float, float]
1589
+ The 3 scale factors in x-, y-, and z-direction.
1590
+ shear : [float, float, float]
1591
+ The 3 shear factors for x-y, x-z, and y-z axes.
1592
+ angles : [float, float, float]
1593
+ The rotation specified through the 3 Euler angles about static x, y, z axes.
1594
+ translation : [float, float, float]
1595
+ The 3 values of translation.
1596
+ perspective : [float, float, float, float]
1597
+ The 4 perspective entries of the matrix.
1598
+
1599
+ Returns
1600
+ -------
1601
+ list[list[float]]
1602
+ The 4x4 matrix that combines the provided transformation components.
1603
+
1604
+ See Also
1605
+ --------
1606
+ decompose_matrix
1607
+
1608
+ Examples
1609
+ --------
1610
+ >>> trans1 = [1, 2, 3]
1611
+ >>> angle1 = [-2.142, 1.141, -0.142]
1612
+ >>> scale1 = [0.123, 2, 0.5]
1613
+ >>> M = compose_matrix(scale1, None, angle1, trans1, None)
1614
+ >>> scale2, shear2, angle2, trans2, persp2 = decompose_matrix(M)
1615
+ >>> allclose(scale1, scale2)
1616
+ True
1617
+ >>> allclose(angle1, angle2)
1618
+ True
1619
+ >>> allclose(trans1, trans2)
1620
+ True
1621
+
1622
+ """
1623
+ M = [[1.0 if i == j else 0.0 for i in range(4)] for j in range(4)]
1624
+ if perspective is not None:
1625
+ P = matrix_from_perspective_entries(perspective)
1626
+ M = multiply_matrices(M, P)
1627
+ if translation is not None:
1628
+ T = matrix_from_translation(translation)
1629
+ M = multiply_matrices(M, T)
1630
+ if angles is not None:
1631
+ R = matrix_from_euler_angles(angles, static=True, axes="xyz")
1632
+ M = multiply_matrices(M, R)
1633
+ if shear is not None:
1634
+ H = matrix_from_shear_entries(shear)
1635
+ M = multiply_matrices(M, H)
1636
+ if scale is not None:
1637
+ S = matrix_from_scale_factors(scale)
1638
+ M = multiply_matrices(M, S)
1639
+ for i in range(4):
1640
+ for j in range(4):
1641
+ M[i][j] /= M[3][3] # type: ignore
1642
+ return M
1643
+
1644
+
1645
+ def identity_matrix(dim):
1646
+ """Construct an identity matrix.
1647
+
1648
+ Parameters
1649
+ ----------
1650
+ dim : int
1651
+ The number of rows and/or columns of the matrix.
1652
+
1653
+ Returns
1654
+ -------
1655
+ list of list
1656
+ A list of `dim` lists, with each list containing `dim` elements.
1657
+ The items on the "diagonal" are one.
1658
+ All other items are zero.
1659
+
1660
+ See Also
1661
+ --------
1662
+ matrix_from_frame
1663
+ matrix_from_frame_to_frame
1664
+ matrix_from_euler_angles
1665
+ matrix_from_axis_and_angle
1666
+ matrix_from_basis_vectors
1667
+ matrix_from_translation
1668
+ matrix_from_scale_factors
1669
+ matrix_from_shear_entries
1670
+ matrix_from_perspective_entries
1671
+
1672
+ Examples
1673
+ --------
1674
+ >>> identity_matrix(4)
1675
+ [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
1676
+
1677
+ """
1678
+ return [[1.0 if i == j else 0.0 for i in range(dim)] for j in range(dim)]
1679
+
1680
+
1681
+ def matrix_from_frame(frame):
1682
+ """Computes a change of basis transformation from world XY to the frame.
1683
+
1684
+ Parameters
1685
+ ----------
1686
+ frame : :class:`compas.geometry.Frame`
1687
+ A frame describing the targeted Cartesian coordinate system
1688
+
1689
+ Returns
1690
+ -------
1691
+ list[list[float]]
1692
+ A 4x4 transformation matrix representing the transformation from
1693
+ world coordinates to frame coordinates.
1694
+
1695
+ Examples
1696
+ --------
1697
+ >>> from compas.geometry import Frame
1698
+ >>> f = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
1699
+ >>> T = matrix_from_frame(f)
1700
+
1701
+ """
1702
+ M = identity_matrix(4)
1703
+ M[0][0], M[1][0], M[2][0] = frame.xaxis
1704
+ M[0][1], M[1][1], M[2][1] = frame.yaxis
1705
+ M[0][2], M[1][2], M[2][2] = frame.zaxis
1706
+ M[0][3], M[1][3], M[2][3] = frame.point
1707
+ return M
1708
+
1709
+
1710
+ def matrix_from_frame_to_frame(frame_from, frame_to):
1711
+ """Computes a transformation between two frames.
1712
+
1713
+ This transformation allows to transform geometry from one Cartesian
1714
+ coordinate system defined by `frame_from` to another Cartesian
1715
+ coordinate system defined by `frame_to`.
1716
+
1717
+ Parameters
1718
+ ----------
1719
+ frame_from : :class:`compas.geometry.Frame`
1720
+ A frame defining the original Cartesian coordinate system
1721
+ frame_to : :class:`compas.geometry.Frame`
1722
+ A frame defining the targeted Cartesian coordinate system
1723
+
1724
+ Returns
1725
+ -------
1726
+ list[list[float]]
1727
+ A 4x4 transformation matrix representing the transformation
1728
+ from one frame to another.
1729
+
1730
+ Examples
1731
+ --------
1732
+ >>> from compas.geometry import Frame
1733
+ >>> f1 = Frame([2, 2, 2], [0.12, 0.58, 0.81], [-0.80, 0.53, -0.26])
1734
+ >>> f2 = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
1735
+ >>> T = matrix_from_frame_to_frame(f1, f2)
1736
+ """
1737
+ T1 = matrix_from_frame(frame_from)
1738
+ T2 = matrix_from_frame(frame_to)
1739
+ return multiply_matrices(T2, matrix_inverse(T1))
1740
+
1741
+
1742
+ def matrix_from_change_of_basis(frame_from, frame_to):
1743
+ """Computes a change of basis transformation between two frames.
1744
+
1745
+ A basis change is essentially a remapping of geometry from one
1746
+ coordinate system to another.
1747
+
1748
+ Parameters
1749
+ ----------
1750
+ frame_from : :class:`compas.geometry.Frame`
1751
+ A frame defining the original Cartesian coordinate system
1752
+ frame_to : :class:`compas.geometry.Frame`
1753
+ A frame defining the targeted Cartesian coordinate system
1754
+
1755
+ Returns
1756
+ -------
1757
+ list[list[float]]
1758
+ A 4x4 transformation matrix representing a change of basis.
1759
+
1760
+ Examples
1761
+ --------
1762
+ >>> from compas.geometry import Point, Frame
1763
+ >>> f1 = Frame([2, 2, 2], [0.12, 0.58, 0.81], [-0.80, 0.53, -0.26])
1764
+ >>> f2 = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
1765
+ >>> T = matrix_from_change_of_basis(f1, f2)
1766
+
1767
+ """
1768
+ T1 = matrix_from_frame(frame_from)
1769
+ T2 = matrix_from_frame(frame_to)
1770
+ return multiply_matrices(matrix_inverse(T2), T1)
1771
+
1772
+
1773
+ def matrix_from_euler_angles(euler_angles, static=True, axes="xyz"):
1774
+ """Calculates a rotation matrix from Euler angles.
1775
+
1776
+ In 3D space any orientation can be achieved by composing three elemental
1777
+ rotations, rotations about the axes (x, y, z) of a coordinate system. A
1778
+ triple of Euler angles can be interpreted in 24 ways, which depends on if
1779
+ the rotations are applied to a static (extrinsic) or rotating (intrinsic)
1780
+ frame and the order of axes.
1781
+
1782
+ Parameters
1783
+ ----------
1784
+ euler_angles : [float, float, float]
1785
+ Three numbers that represent the angles of rotations about the defined axes.
1786
+ static : bool, optional
1787
+ If True the rotations are applied to a static frame.
1788
+ If False, to a rotational.
1789
+ axes : Literal['xyz', 'yzx', 'zxy'], optional
1790
+ A 3 character string specifying order of the axes.
1791
+
1792
+ Returns
1793
+ -------
1794
+ list[list[float]]
1795
+ A 4x4 transformation matrix representing a rotation.
1796
+
1797
+ Examples
1798
+ --------
1799
+ >>> ea1 = 1.4, 0.5, 2.3
1800
+ >>> R = matrix_from_euler_angles(ea1)
1801
+ >>> ea2 = euler_angles_from_matrix(R)
1802
+ >>> allclose(ea1, ea2)
1803
+ True
1804
+
1805
+ """
1806
+ global _SPEC2TUPLE
1807
+ global _NEXT_SPEC
1808
+
1809
+ ai, aj, ak = euler_angles
1810
+
1811
+ if static:
1812
+ firstaxis, parity, repetition, frame = _SPEC2TUPLE["s" + axes]
1813
+ else:
1814
+ firstaxis, parity, repetition, frame = _SPEC2TUPLE["r" + axes]
1815
+
1816
+ i = firstaxis
1817
+ j = _NEXT_SPEC[i + parity]
1818
+ k = _NEXT_SPEC[i - parity + 1]
1819
+
1820
+ if frame:
1821
+ ai, ak = ak, ai
1822
+ if parity:
1823
+ ai, aj, ak = -ai, -aj, -ak
1824
+
1825
+ si, sj, sk = sin(ai), sin(aj), sin(ak)
1826
+ ci, cj, ck = cos(ai), cos(aj), cos(ak)
1827
+ cc, cs = ci * ck, ci * sk
1828
+ sc, ss = si * ck, si * sk
1829
+
1830
+ M = [[1.0 if x == y else 0.0 for x in range(4)] for y in range(4)]
1831
+
1832
+ if repetition:
1833
+ M[i][i] = cj
1834
+ M[i][j] = sj * si
1835
+ M[i][k] = sj * ci
1836
+ M[j][i] = sj * sk
1837
+ M[j][j] = -cj * ss + cc
1838
+ M[j][k] = -cj * cs - sc
1839
+ M[k][i] = -sj * ck
1840
+ M[k][j] = cj * sc + cs
1841
+ M[k][k] = cj * cc - ss
1842
+ else:
1843
+ M[i][i] = cj * ck
1844
+ M[i][j] = sj * sc - cs
1845
+ M[i][k] = sj * cc + ss
1846
+ M[j][i] = cj * sk
1847
+ M[j][j] = sj * ss + cc
1848
+ M[j][k] = sj * cs - sc
1849
+ M[k][i] = -sj
1850
+ M[k][j] = cj * si
1851
+ M[k][k] = cj * ci
1852
+
1853
+ return M
1854
+
1855
+
1856
+ def euler_angles_from_matrix(M, static=True, axes="xyz"):
1857
+ """Returns Euler angles from the rotation matrix M according to specified
1858
+ axis sequence and type of rotation.
1859
+
1860
+ Parameters
1861
+ ----------
1862
+ M : list[list[float]]
1863
+ The 3x3 or 4x4 matrix in row-major order.
1864
+ static : bool, optional
1865
+ If True the rotations are applied to a static frame.
1866
+ If False, to a rotational.
1867
+ axes : str, optional
1868
+ A 3 character string specifying order of the axes.
1869
+
1870
+ Returns
1871
+ -------
1872
+ list[float]
1873
+ The 3 Euler angles.
1874
+
1875
+ Examples
1876
+ --------
1877
+ >>> ea1 = 1.4, 0.5, 2.3
1878
+ >>> R = matrix_from_euler_angles(ea1)
1879
+ >>> ea2 = euler_angles_from_matrix(R)
1880
+ >>> allclose(ea1, ea2)
1881
+ True
1882
+
1883
+ """
1884
+ global _SPEC2TUPLE
1885
+ global _NEXT_SPEC
1886
+
1887
+ if static:
1888
+ firstaxis, parity, repetition, frame = _SPEC2TUPLE["s" + axes]
1889
+ else:
1890
+ firstaxis, parity, repetition, frame = _SPEC2TUPLE["r" + axes]
1891
+
1892
+ i = firstaxis
1893
+ j = _NEXT_SPEC[i + parity]
1894
+ k = _NEXT_SPEC[i - parity + 1]
1895
+
1896
+ if repetition:
1897
+ sy = sqrt(M[i][j] * M[i][j] + M[i][k] * M[i][k])
1898
+ if TOL.is_positive(sy):
1899
+ ax = atan2(M[i][j], M[i][k])
1900
+ ay = atan2(sy, M[i][i])
1901
+ az = atan2(M[j][i], -M[k][i])
1902
+ else:
1903
+ ax = atan2(-M[j][k], M[j][j])
1904
+ ay = atan2(sy, M[i][i])
1905
+ az = 0.0
1906
+ else:
1907
+ cy = sqrt(M[i][i] * M[i][i] + M[j][i] * M[j][i])
1908
+ if TOL.is_positive(cy):
1909
+ ax = atan2(M[k][j], M[k][k])
1910
+ ay = atan2(-M[k][i], cy)
1911
+ az = atan2(M[j][i], M[i][i])
1912
+ else:
1913
+ ax = atan2(-M[j][k], M[j][j])
1914
+ ay = atan2(-M[k][i], cy)
1915
+ az = 0.0
1916
+
1917
+ if parity:
1918
+ ax, ay, az = -ax, -ay, -az
1919
+ if frame:
1920
+ ax, az = az, ax
1921
+
1922
+ return [ax, ay, az]
1923
+
1924
+
1925
+ def matrix_from_axis_and_angle(axis, angle, point=None):
1926
+ """Calculates a rotation matrix from an rotation axis, an angle and an optional
1927
+ point of rotation.
1928
+
1929
+ Parameters
1930
+ ----------
1931
+ axis : [float, float, float]
1932
+ Three numbers that represent the axis of rotation.
1933
+ angle : float
1934
+ The rotation angle in radians.
1935
+ point : [float, float, float] | :class:`compas.geometry.Point`, optional
1936
+ A point to perform a rotation around an origin other than [0, 0, 0].
1937
+
1938
+ Returns
1939
+ -------
1940
+ list[list[float]]
1941
+ A 4x4 transformation matrix representing a rotation.
1942
+
1943
+ Notes
1944
+ -----
1945
+ The rotation is based on the right hand rule, i.e. anti-clockwise if the
1946
+ axis of rotation points towards the observer.
1947
+
1948
+ Examples
1949
+ --------
1950
+ >>> axis1 = normalize_vector([-0.043, -0.254, 0.617])
1951
+ >>> angle1 = 0.1
1952
+ >>> R = matrix_from_axis_and_angle(axis1, angle1)
1953
+ >>> axis2, angle2 = axis_and_angle_from_matrix(R)
1954
+ >>> allclose(axis1, axis2)
1955
+ True
1956
+ >>> allclose([angle1], [angle2])
1957
+ True
1958
+
1959
+ """
1960
+ if not point:
1961
+ point = [0.0, 0.0, 0.0]
1962
+
1963
+ axis = list(axis)
1964
+ if length_vector(axis):
1965
+ axis = normalize_vector(axis)
1966
+
1967
+ sina = sin(angle)
1968
+ cosa = cos(angle)
1969
+
1970
+ R = [[cosa, 0.0, 0.0], [0.0, cosa, 0.0], [0.0, 0.0, cosa]]
1971
+
1972
+ outer_product = [[axis[i] * axis[j] * (1.0 - cosa) for i in range(3)] for j in range(3)]
1973
+ R = [[R[i][j] + outer_product[i][j] for i in range(3)] for j in range(3)]
1974
+
1975
+ axis = scale_vector(axis, sina)
1976
+ m = [[0.0, -axis[2], axis[1]], [axis[2], 0.0, -axis[0]], [-axis[1], axis[0], 0.0]]
1977
+
1978
+ M = identity_matrix(4)
1979
+
1980
+ for i in range(3):
1981
+ for j in range(3):
1982
+ R[i][j] += m[i][j]
1983
+ M[i][j] = R[i][j]
1984
+
1985
+ # rotation about axis, angle AND point includes also translation
1986
+ t = subtract_vectors(point, multiply_matrix_vector(R, point))
1987
+ M[0][3] = t[0]
1988
+ M[1][3] = t[1]
1989
+ M[2][3] = t[2]
1990
+
1991
+ return M
1992
+
1993
+
1994
+ def matrix_from_axis_angle_vector(axis_angle_vector, point=[0, 0, 0]):
1995
+ """Calculates a rotation matrix from an axis-angle vector.
1996
+
1997
+ Parameters
1998
+ ----------
1999
+ axis_angle_vector : [float, float, float]
2000
+ Three numbers that represent the axis of rotation and angle of rotation
2001
+ through the vector's magnitude.
2002
+ point : [float, float, float] | :class:`compas.geometry.Point`, optional
2003
+ A point to perform a rotation around an origin other than [0, 0, 0].
2004
+
2005
+ Returns
2006
+ -------
2007
+ list[list[float]]
2008
+ The 4x4 transformation matrix representing a rotation.
2009
+
2010
+ Examples
2011
+ --------
2012
+ >>> aav1 = [-0.043, -0.254, 0.617]
2013
+ >>> R = matrix_from_axis_angle_vector(aav1)
2014
+ >>> aav2 = axis_angle_vector_from_matrix(R)
2015
+ >>> allclose(aav1, aav2)
2016
+ True
2017
+
2018
+ """
2019
+ axis = list(axis_angle_vector)
2020
+ angle = length_vector(axis_angle_vector)
2021
+ return matrix_from_axis_and_angle(axis, angle, point)
2022
+
2023
+
2024
+ def axis_and_angle_from_matrix(M):
2025
+ """Returns the axis and the angle of the rotation matrix M.
2026
+
2027
+ Parameters
2028
+ ----------
2029
+ M : list[list[float]]
2030
+ The 4-by-4 transformation matrix.
2031
+
2032
+ Returns
2033
+ -------
2034
+ [float, float, float]
2035
+ The rotation axis.
2036
+ float
2037
+ The rotation angle in radians.
2038
+
2039
+ """
2040
+ eps = 0.01 # margin to allow for rounding errors
2041
+ eps2 = 0.1 # margin to distinguish between 0 and 180 degrees
2042
+
2043
+ if all(fabs(M[i][j] - M[j][i]) < eps for i, j in [(0, 1), (0, 2), (1, 2)]):
2044
+ if all(fabs(M[i][j] - M[j][i]) < eps2 for i, j in [(0, 1), (0, 2), (1, 2)]) and fabs(M[0][0] + M[1][1] + M[2][2] - 3) < eps2:
2045
+ return [0, 0, 0], 0
2046
+
2047
+ angle = pi
2048
+ xx = (M[0][0] + 1) / 2
2049
+ yy = (M[1][1] + 1) / 2
2050
+ zz = (M[2][2] + 1) / 2
2051
+ xy = (M[0][1] + M[1][0]) / 4
2052
+ xz = (M[0][2] + M[2][0]) / 4
2053
+ yz = (M[1][2] + M[2][1]) / 4
2054
+ root_half = sqrt(0.5)
2055
+ if (xx > yy) and (xx > zz):
2056
+ if xx < eps:
2057
+ axis = [0, root_half, root_half]
2058
+ else:
2059
+ x = sqrt(xx)
2060
+ axis = [x, xy / x, xz / x]
2061
+ elif yy > zz:
2062
+ if yy < eps:
2063
+ axis = [root_half, 0, root_half]
2064
+ else:
2065
+ y = sqrt(yy)
2066
+ axis = [xy / y, y, yz / y]
2067
+ else:
2068
+ if zz < eps:
2069
+ axis = [root_half, root_half, 0]
2070
+ else:
2071
+ z = sqrt(zz)
2072
+ axis = [xz / z, yz / z, z]
2073
+
2074
+ return axis, angle
2075
+
2076
+ s = sqrt((M[2][1] - M[1][2]) * (M[2][1] - M[1][2]) + (M[0][2] - M[2][0]) * (M[0][2] - M[2][0]) + (M[1][0] - M[0][1]) * (M[1][0] - M[0][1]))
2077
+
2078
+ # should this also be an eps?
2079
+ if fabs(s) < 0.001:
2080
+ s = 1
2081
+
2082
+ angle = acos((M[0][0] + M[1][1] + M[2][2] - 1) / 2)
2083
+
2084
+ x = (M[2][1] - M[1][2]) / s
2085
+ y = (M[0][2] - M[2][0]) / s
2086
+ z = (M[1][0] - M[0][1]) / s
2087
+
2088
+ return [x, y, z], angle
2089
+
2090
+
2091
+ def axis_angle_vector_from_matrix(M):
2092
+ """Returns the axis-angle vector of the rotation matrix M.
2093
+
2094
+ Parameters
2095
+ ----------
2096
+ M : list[list[float]]
2097
+ The 4-by-4 transformation matrix.
2098
+
2099
+ Returns
2100
+ -------
2101
+ [float, float, float]
2102
+ The axis-angle vector.
2103
+
2104
+ """
2105
+ axis, angle = axis_and_angle_from_matrix(M)
2106
+ return scale_vector(axis, angle)
2107
+
2108
+
2109
+ def matrix_from_quaternion(quaternion):
2110
+ """Calculates a rotation matrix from quaternion coefficients.
2111
+
2112
+ Parameters
2113
+ ----------
2114
+ quaternion : [float, float, float, float]
2115
+ Four numbers that represents the four coefficient values of a quaternion.
2116
+
2117
+ Returns
2118
+ -------
2119
+ list[list[float]]
2120
+ The 4x4 transformation matrix representing a rotation.
2121
+
2122
+ Raises
2123
+ ------
2124
+ ValueError
2125
+ If quaternion is invalid.
2126
+
2127
+ Examples
2128
+ --------
2129
+ >>> q1 = [0.945, -0.021, -0.125, 0.303]
2130
+ >>> R = matrix_from_quaternion(q1)
2131
+ >>> q2 = quaternion_from_matrix(R)
2132
+ >>> allclose(q1, q2, tol=1e-03)
2133
+ True
2134
+
2135
+ """
2136
+ q = quaternion
2137
+ n = q[0] ** 2 + q[1] ** 2 + q[2] ** 2 + q[3] ** 2 # dot product
2138
+
2139
+ # perhaps this should not be hard-coded?
2140
+ eps = 1.0e-15
2141
+
2142
+ if n < eps:
2143
+ raise ValueError("Invalid quaternion, dot product must be != 0.")
2144
+
2145
+ q = [v * sqrt(2.0 / n) for v in q]
2146
+ q = [[q[i] * q[j] for i in range(4)] for j in range(4)] # outer_product
2147
+
2148
+ rotation = [
2149
+ [1.0 - q[2][2] - q[3][3], q[1][2] - q[3][0], q[1][3] + q[2][0], 0.0],
2150
+ [q[1][2] + q[3][0], 1.0 - q[1][1] - q[3][3], q[2][3] - q[1][0], 0.0],
2151
+ [q[1][3] - q[2][0], q[2][3] + q[1][0], 1.0 - q[1][1] - q[2][2], 0.0],
2152
+ [0.0, 0.0, 0.0, 1.0],
2153
+ ]
2154
+ return rotation
2155
+
2156
+
2157
+ def quaternion_from_matrix(M):
2158
+ """Returns the 4 quaternion coefficients from a rotation matrix.
2159
+
2160
+ Parameters
2161
+ ----------
2162
+ M : list[list[float]]
2163
+ The coefficients of the rotation matrix, row per row.
2164
+
2165
+ Returns
2166
+ -------
2167
+ [float, float, float, float]
2168
+ The quaternion coefficients.
2169
+
2170
+ Examples
2171
+ --------
2172
+ >>> q1 = [0.945, -0.021, -0.125, 0.303]
2173
+ >>> R = matrix_from_quaternion(q1)
2174
+ >>> q2 = quaternion_from_matrix(R)
2175
+ >>> allclose(q1, q2, tol=1e-03)
2176
+ True
2177
+
2178
+ """
2179
+ qw, qx, qy, qz = 0, 0, 0, 0
2180
+ trace = M[0][0] + M[1][1] + M[2][2]
2181
+
2182
+ if trace > 0.0:
2183
+ s = 0.5 / sqrt(trace + 1.0)
2184
+ qw = 0.25 / s
2185
+ qx = (M[2][1] - M[1][2]) * s
2186
+ qy = (M[0][2] - M[2][0]) * s
2187
+ qz = (M[1][0] - M[0][1]) * s
2188
+
2189
+ elif (M[0][0] > M[1][1]) and (M[0][0] > M[2][2]):
2190
+ s = 2.0 * sqrt(1.0 + M[0][0] - M[1][1] - M[2][2])
2191
+ qw = (M[2][1] - M[1][2]) / s
2192
+ qx = 0.25 * s
2193
+ qy = (M[0][1] + M[1][0]) / s
2194
+ qz = (M[0][2] + M[2][0]) / s
2195
+
2196
+ elif M[1][1] > M[2][2]:
2197
+ s = 2.0 * sqrt(1.0 + M[1][1] - M[0][0] - M[2][2])
2198
+ qw = (M[0][2] - M[2][0]) / s
2199
+ qx = (M[0][1] + M[1][0]) / s
2200
+ qy = 0.25 * s
2201
+ qz = (M[1][2] + M[2][1]) / s
2202
+ else:
2203
+ s = 2.0 * sqrt(1.0 + M[2][2] - M[0][0] - M[1][1])
2204
+ qw = (M[1][0] - M[0][1]) / s
2205
+ qx = (M[0][2] + M[2][0]) / s
2206
+ qy = (M[1][2] + M[2][1]) / s
2207
+ qz = 0.25 * s
2208
+
2209
+ return [qw, qx, qy, qz]
2210
+
2211
+
2212
+ def matrix_from_basis_vectors(xaxis, yaxis):
2213
+ """Creates a rotation matrix from basis vectors (= orthonormal vectors).
2214
+
2215
+ Parameters
2216
+ ----------
2217
+ xaxis : [float, float, float] | :class:`compas.geometry.Vector`
2218
+ The x-axis of the frame.
2219
+ yaxis : [float, float, float] | :class:`compas.geometry.Vector`
2220
+ The y-axis of the frame.
2221
+
2222
+ Returns
2223
+ -------
2224
+ list[list[float]]
2225
+ A 4x4 transformation matrix representing a rotation.
2226
+
2227
+ Notes
2228
+ -----
2229
+ .. code-block:: none
2230
+
2231
+ [ x0 y0 z0 0 ]
2232
+ [ x1 y1 z1 0 ]
2233
+ [ x2 y2 z2 0 ]
2234
+ [ 0 0 0 1 ]
2235
+
2236
+ Examples
2237
+ --------
2238
+ >>> xaxis = [0.68, 0.68, 0.27]
2239
+ >>> yaxis = [-0.67, 0.73, -0.15]
2240
+ >>> R = matrix_from_basis_vectors(xaxis, yaxis)
2241
+
2242
+ """
2243
+ xaxis = normalize_vector(list(xaxis))
2244
+ yaxis = normalize_vector(list(yaxis))
2245
+ zaxis = cross_vectors(xaxis, yaxis)
2246
+ yaxis = cross_vectors(zaxis, xaxis)
2247
+
2248
+ R = identity_matrix(4)
2249
+ R[0][0], R[1][0], R[2][0] = xaxis
2250
+ R[0][1], R[1][1], R[2][1] = yaxis
2251
+ R[0][2], R[1][2], R[2][2] = zaxis
2252
+ return R
2253
+
2254
+
2255
+ def basis_vectors_from_matrix(R):
2256
+ """Returns the basis vectors from the rotation matrix R.
2257
+
2258
+ Parameters
2259
+ ----------
2260
+ R : list[list[float]]
2261
+ A 4-by-4 transformation matrix, or a 3-by-3 rotation matrix.
2262
+
2263
+ Returns
2264
+ -------
2265
+ [float, float, float]
2266
+ The first basis vector of the rotation.
2267
+ [float, float, float]
2268
+ The second basis vector of the rotation.
2269
+
2270
+ Raises
2271
+ ------
2272
+ ValueError
2273
+ If rotation matrix is invalid.
2274
+
2275
+ Examples
2276
+ --------
2277
+ >>> from compas.geometry import Frame
2278
+ >>> f = Frame([0, 0, 0], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
2279
+ >>> R = matrix_from_frame(f)
2280
+ >>> xaxis, yaxis = basis_vectors_from_matrix(R)
2281
+
2282
+ """
2283
+ xaxis = [R[0][0], R[1][0], R[2][0]]
2284
+ yaxis = [R[0][1], R[1][1], R[2][1]]
2285
+ zaxis = [R[0][2], R[1][2], R[2][2]]
2286
+
2287
+ if not allclose(zaxis, cross_vectors(xaxis, yaxis)):
2288
+ raise ValueError("Matrix is invalid rotation matrix.")
2289
+
2290
+ return xaxis, yaxis
2291
+
2292
+
2293
+ def matrix_from_translation(translation):
2294
+ """Returns a 4x4 translation matrix in row-major order.
2295
+
2296
+ Parameters
2297
+ ----------
2298
+ translation : [float, float, float]
2299
+ The x, y and z components of the translation.
2300
+
2301
+ Returns
2302
+ -------
2303
+ list[list[float]]
2304
+ The 4x4 transformation matrix representing a translation.
2305
+
2306
+ Notes
2307
+ -----
2308
+ .. code-block:: none
2309
+
2310
+ [ . . . 0 ]
2311
+ [ . . . 1 ]
2312
+ [ . . . 2 ]
2313
+ [ . . . . ]
2314
+
2315
+ Examples
2316
+ --------
2317
+ >>> T = matrix_from_translation([1, 2, 3])
2318
+
2319
+ """
2320
+ M = identity_matrix(4)
2321
+ M[0][3] = float(translation[0])
2322
+ M[1][3] = float(translation[1])
2323
+ M[2][3] = float(translation[2])
2324
+
2325
+ return M
2326
+
2327
+
2328
+ def translation_from_matrix(M):
2329
+ """Returns the 3 values of translation from the matrix M.
2330
+
2331
+ Parameters
2332
+ ----------
2333
+ M : list[list[float]]
2334
+ A 4-by-4 transformation matrix.
2335
+
2336
+ Returns
2337
+ -------
2338
+ [float, float, float]
2339
+ The translation vector.
2340
+
2341
+ """
2342
+ return [M[0][3], M[1][3], M[2][3]]
2343
+
2344
+
2345
+ def matrix_from_orthogonal_projection(plane):
2346
+ """Returns an orthogonal projection matrix to project onto a plane.
2347
+
2348
+ Parameters
2349
+ ----------
2350
+ plane : [point, normal]
2351
+ The plane to project onto.
2352
+
2353
+ Returns
2354
+ -------
2355
+ list[list[float]]
2356
+ The 4x4 transformation matrix representing an orthogonal projection.
2357
+
2358
+ Examples
2359
+ --------
2360
+ >>> point = [0, 0, 0]
2361
+ >>> normal = [0, 0, 1]
2362
+ >>> plane = (point, normal)
2363
+ >>> P = matrix_from_orthogonal_projection(plane)
2364
+
2365
+ """
2366
+ point, normal = plane
2367
+ T = identity_matrix(4)
2368
+ normal = normalize_vector(normal)
2369
+
2370
+ for j in range(3):
2371
+ for i in range(3):
2372
+ T[i][j] -= normal[i] * normal[j] # outer_product
2373
+
2374
+ T[0][3], T[1][3], T[2][3] = scale_vector(normal, dot_vectors(point, normal))
2375
+ return T
2376
+
2377
+
2378
+ def matrix_from_parallel_projection(plane, direction):
2379
+ """Returns an parallel projection matrix to project onto a plane.
2380
+
2381
+ Parameters
2382
+ ----------
2383
+ plane : [point, normal]
2384
+ The plane to project onto.
2385
+ direction : [float, float, float] | :class:`compas.geometry.Vector`
2386
+ Direction of the projection.
2387
+
2388
+ Returns
2389
+ -------
2390
+ list[list[float]]
2391
+ A 4-by-4 transformation matrix.
2392
+
2393
+ Examples
2394
+ --------
2395
+ >>> point = [0, 0, 0]
2396
+ >>> normal = [0, 0, 1]
2397
+ >>> plane = (point, normal)
2398
+ >>> direction = [1, 1, 1]
2399
+ >>> P = matrix_from_parallel_projection(plane, direction)
2400
+
2401
+ """
2402
+ point, normal = plane
2403
+ T = identity_matrix(4)
2404
+ normal = normalize_vector(normal)
2405
+
2406
+ scale = dot_vectors(direction, normal)
2407
+ for j in range(3):
2408
+ for i in range(3):
2409
+ T[i][j] -= direction[i] * normal[j] / scale
2410
+
2411
+ T[0][3], T[1][3], T[2][3] = scale_vector(direction, dot_vectors(point, normal) / scale)
2412
+ return T
2413
+
2414
+
2415
+ def matrix_from_perspective_projection(plane, center_of_projection):
2416
+ """Returns a perspective projection matrix to project onto a plane along lines that emanate from a single point, called the center of projection.
2417
+
2418
+ Parameters
2419
+ ----------
2420
+ plane : [point, normal]
2421
+ The plane to project onto.
2422
+ center_of_projection : [float, float, float] | :class:`compas.geometry.Point`
2423
+ The camera view point.
2424
+
2425
+ Returns
2426
+ -------
2427
+ list[list[float]]
2428
+ A 4-by-4 transformation matrix.
2429
+
2430
+ Examples
2431
+ --------
2432
+ >>> point = [0, 0, 0]
2433
+ >>> normal = [0, 0, 1]
2434
+ >>> plane = (point, normal)
2435
+ >>> center_of_projection = [1, 1, 0]
2436
+ >>> P = matrix_from_perspective_projection(plane, center_of_projection)
2437
+
2438
+ """
2439
+ point, normal = plane
2440
+ T = identity_matrix(4)
2441
+ normal = normalize_vector(normal)
2442
+
2443
+ T[0][0] = T[1][1] = T[2][2] = dot_vectors(subtract_vectors(center_of_projection, point), normal)
2444
+
2445
+ for j in range(3):
2446
+ for i in range(3):
2447
+ T[i][j] -= center_of_projection[i] * normal[j]
2448
+
2449
+ T[0][3], T[1][3], T[2][3] = scale_vector(center_of_projection, dot_vectors(point, normal))
2450
+
2451
+ for i in range(3):
2452
+ T[3][i] -= normal[i]
2453
+
2454
+ T[3][3] = dot_vectors(center_of_projection, normal)
2455
+
2456
+ return T
2457
+
2458
+
2459
+ def matrix_from_perspective_entries(perspective):
2460
+ """Returns a matrix from perspective entries.
2461
+
2462
+ Parameters
2463
+ ----------
2464
+ values : [float, float, float, float]
2465
+ The 4 perspective entries of a matrix.
2466
+
2467
+ Returns
2468
+ -------
2469
+ list[list[float]]
2470
+ A 4-by-4 transformation matrix.
2471
+
2472
+ Notes
2473
+ -----
2474
+ .. code-block:: none
2475
+
2476
+ [ . . . . ]
2477
+ [ . . . . ]
2478
+ [ . . . . ]
2479
+ [ 0 1 2 3 ]
2480
+
2481
+ """
2482
+ M = identity_matrix(4)
2483
+ M[3][0] = float(perspective[0])
2484
+ M[3][1] = float(perspective[1])
2485
+ M[3][2] = float(perspective[2])
2486
+ M[3][3] = float(perspective[3])
2487
+ return M
2488
+
2489
+
2490
+ def matrix_from_shear_entries(shear_entries):
2491
+ """Returns a shear matrix from the 3 factors for x-y, x-z, and y-z axes.
2492
+
2493
+ Parameters
2494
+ ----------
2495
+ shear_entries : [float, float, float]
2496
+ The 3 shear factors for x-y, x-z, and y-z axes.
2497
+
2498
+ Returns
2499
+ -------
2500
+ list[list[float]]
2501
+ A 4-by-4 transformation matrix.
2502
+
2503
+ Notes
2504
+ -----
2505
+ .. code-block:: none
2506
+
2507
+ [ . 0 1 . ]
2508
+ [ . . 2 . ]
2509
+ [ . . . . ]
2510
+ [ . . . . ]
2511
+
2512
+ Examples
2513
+ --------
2514
+ >>> Sh = matrix_from_shear_entries([1, 2, 3])
2515
+
2516
+ """
2517
+ M = identity_matrix(4)
2518
+ M[0][1] = float(shear_entries[0])
2519
+ M[0][2] = float(shear_entries[1])
2520
+ M[1][2] = float(shear_entries[2])
2521
+ return M
2522
+
2523
+
2524
+ def matrix_from_shear(angle, direction, point, normal):
2525
+ """Constructs a shear matrix by an angle along the direction vector on the
2526
+ shear plane (defined by point and normal).
2527
+
2528
+ Parameters
2529
+ ----------
2530
+ angle : float
2531
+ The angle in radians.
2532
+ direction : [float, float, float] | :class:`compas.geometry.Vector`
2533
+ The direction vector as list of 3 numbers.
2534
+ It must be orthogonal to the normal vector.
2535
+ point : [float, float, float] | :class:`compas.geometry.Point`
2536
+ The point of the shear plane as list of 3 numbers.
2537
+ normal : [float, float, float] | :class:`compas.geometry.Vector`
2538
+ The normal of the shear plane as list of 3 numbers.
2539
+
2540
+ Returns
2541
+ -------
2542
+ list[list[float]]
2543
+ A 4-by-4 transformation matrix.
2544
+
2545
+ Raises
2546
+ ------
2547
+ ValueError
2548
+ If direction and normal are not orthogonal.
2549
+
2550
+ Notes
2551
+ -----
2552
+ A point P is transformed by the shear matrix into P" such that
2553
+ the vector P-P" is parallel to the direction vector and its extent is
2554
+ given by the angle of P-P'-P", where P' is the orthogonal projection
2555
+ of P onto the shear plane (defined by point and normal).
2556
+
2557
+ Examples
2558
+ --------
2559
+ >>> angle = 0.1
2560
+ >>> direction = [0.1, 0.2, 0.3]
2561
+ >>> point = [4, 3, 1]
2562
+ >>> normal = cross_vectors(direction, [1, 0.3, -0.1])
2563
+ >>> S = matrix_from_shear(angle, direction, point, normal)
2564
+
2565
+ """
2566
+ normal = normalize_vector(normal)
2567
+ direction = normalize_vector(direction)
2568
+
2569
+ if not TOL.is_zero(dot_vectors(normal, direction)):
2570
+ raise ValueError("Direction and normal vectors are not orthogonal")
2571
+
2572
+ angle = tan(angle)
2573
+ M = identity_matrix(4)
2574
+
2575
+ for j in range(3):
2576
+ for i in range(3):
2577
+ M[i][j] += angle * direction[i] * normal[j]
2578
+
2579
+ M[0][3], M[1][3], M[2][3] = scale_vector(direction, -angle * dot_vectors(point, normal))
2580
+
2581
+ return M
2582
+
2583
+
2584
+ def matrix_from_scale_factors(scale_factors):
2585
+ """Returns a 4x4 scaling transformation.
2586
+
2587
+ Parameters
2588
+ ----------
2589
+ scale_factors : [float, float, float]
2590
+ Three numbers defining the scaling factors in x, y, and z respectively.
2591
+
2592
+ Returns
2593
+ -------
2594
+ list[list[float]]
2595
+ A 4-by-4 transformation matrix.
2596
+
2597
+ Notes
2598
+ -----
2599
+ .. code-block:: python
2600
+
2601
+ [ 0 . . . ]
2602
+ [ . 1 . . ]
2603
+ [ . . 2 . ]
2604
+ [ . . . . ]
2605
+
2606
+ Examples
2607
+ --------
2608
+ >>> Sc = matrix_from_scale_factors([1, 2, 3])
2609
+
2610
+ """
2611
+ M = identity_matrix(4)
2612
+ M[0][0] = float(scale_factors[0])
2613
+ M[1][1] = float(scale_factors[1])
2614
+ M[2][2] = float(scale_factors[2])
2615
+
2616
+ return M
2617
+
2618
+
2619
+ def quaternion_from_euler_angles(e, static=True, axes="xyz"):
2620
+ """Returns a quaternion from Euler angles.
2621
+
2622
+ Parameters
2623
+ ----------
2624
+ euler_angles : [float, float, float]
2625
+ Three numbers that represent the angles of rotations about the specified axes.
2626
+ static : bool, optional
2627
+ If True, the rotations are applied to a static frame.
2628
+ If False, the rotations are applied to a rotational frame.
2629
+ axes : str, optional
2630
+ A three-character string specifying the order of the axes.
2631
+
2632
+ Returns
2633
+ -------
2634
+ [float, float, float, float]
2635
+ Quaternion as a list of four real values ``[w, x, y, z]``.
2636
+
2637
+ """
2638
+ m = matrix_from_euler_angles(e, static, axes)
2639
+ q = quaternion_from_matrix(m)
2640
+ return q
2641
+
2642
+
2643
+ def euler_angles_from_quaternion(q, static=True, axes="xyz"):
2644
+ """Returns Euler angles from a quaternion.
2645
+
2646
+ Parameters
2647
+ ----------
2648
+ quaternion : [float, float, float, float]
2649
+ Quaternion as a list of four real values ``[w, x, y, z]``.
2650
+ static : bool, optional
2651
+ If True, the rotations are applied to a static frame.
2652
+ If False, the rotations are applied to a rotational frame.
2653
+ axes : str, optional
2654
+ A three-character string specifying the order of the axes.
2655
+
2656
+ Returns
2657
+ -------
2658
+ [float, float, float]
2659
+ Euler angles as a list of three real values ``[a, b, c]``.
2660
+
2661
+ """
2662
+ m = matrix_from_quaternion(q)
2663
+ e = euler_angles_from_matrix(m, static, axes)
2664
+ return e
2665
+
2666
+
2667
+ def quaternion_from_axis_angle(axis, angle):
2668
+ """Returns a quaternion describing a rotation around the given axis by the given angle.
2669
+
2670
+ Parameters
2671
+ ----------
2672
+ axis : [float, float, float] | :class:`compas.geometry.Vector`
2673
+ XYZ coordinates of the rotation axis vector.
2674
+ angle : float
2675
+ Angle of rotation in radians.
2676
+
2677
+ Returns
2678
+ -------
2679
+ [float, float, float, float]
2680
+ Quaternion as a list of four real values ``[qw, qx, qy, qz]``.
2681
+
2682
+ Examples
2683
+ --------
2684
+ >>> axis = [1.0, 0.0, 0.0]
2685
+ >>> angle = math.pi / 2
2686
+ >>> q = quaternion_from_axis_angle(axis, angle)
2687
+ >>> allclose(q, [math.sqrt(2) / 2, math.sqrt(2) / 2, 0, 0])
2688
+ True
2689
+
2690
+ """
2691
+ m = matrix_from_axis_and_angle(axis, angle, None)
2692
+ q = quaternion_from_matrix(m)
2693
+ return q
2694
+
2695
+
2696
+ def axis_angle_from_quaternion(q):
2697
+ """Returns an axis and an angle of rotation from the given quaternion.
2698
+
2699
+ Parameters
2700
+ ----------
2701
+ q : [float, float, float, float]
2702
+ Quaternion as a list of four real values ``[qw, qx, qy, qz]``.
2703
+
2704
+ Returns
2705
+ -------
2706
+ axis : [float, float, float]
2707
+ XYZ coordinates of the rotation axis vector.
2708
+ angle : float
2709
+ Angle of rotation in radians.
2710
+
2711
+ Examples
2712
+ --------
2713
+ >>> q = [1.0, 1.0, 0.0, 0.0]
2714
+ >>> axis, angle = axis_angle_from_quaternion(q)
2715
+ >>> allclose(axis, [1.0, 0.0, 0.0])
2716
+ True
2717
+ >>> allclose([angle], [math.pi / 2], 1e-6)
2718
+ True
2719
+
2720
+ """
2721
+ m = matrix_from_quaternion(q)
2722
+ axis, angle = axis_and_angle_from_matrix(m)
2723
+ return axis, angle
2724
+
2725
+
2726
+ # =============================================================================
2727
+ # =============================================================================
2728
+ # =============================================================================
2729
+ # =============================================================================
2730
+ # =============================================================================
2731
+ # =============================================================================
2732
+ # =============================================================================
2733
+ # Deprecated
2734
+ # =============================================================================
2735
+ # =============================================================================
2736
+ # =============================================================================
2737
+ # =============================================================================
2738
+ # =============================================================================
2739
+ # =============================================================================
2740
+ # =============================================================================
2741
+
2742
+
2743
+ def close(value1, value2, tol=1e-05):
2744
+ """Returns True if two values are equal within a tolerance.
2745
+
2746
+ Parameters
2747
+ ----------
2748
+ value1 : float or int
2749
+ value2 : float or int
2750
+ tol : float, optional
2751
+ The absolute tolerance for comparing values.
2752
+ Default is :attr:`TOL.absolute`.
2753
+
2754
+ Returns
2755
+ -------
2756
+ bool
2757
+ True if the values are closer than the tolerance.
2758
+ False otherwise.
2759
+
2760
+ Warnings
2761
+ --------
2762
+ .. deprecated:: 2.0
2763
+ Will be removed in 2.1
2764
+ Use :func:`TOL.is_close` instead.
2765
+
2766
+ The tolerance value used by this function is an absolute tolerance.
2767
+ It is more accurate to use a combination of absolute and relative tolerance.
2768
+ Therefor, use :func:`TOL.is_close` instead.
2769
+
2770
+ Examples
2771
+ --------
2772
+ >>> close(1.0, 1.001)
2773
+ False
2774
+ >>> close(1.0, 1.001, tol=1e-2)
2775
+ True
2776
+ """
2777
+ return TOL.is_close(value1, value2, rtol=0.0, atol=tol)
2778
+
2779
+
2780
+ def allclose(l1, l2, tol=None):
2781
+ """Returns True if two lists are element-wise equal within a tolerance.
2782
+
2783
+ Parameters
2784
+ ----------
2785
+ l1 : sequence[float]
2786
+ The first list of values.
2787
+ l2 : sequence[float]
2788
+ The second list of values.
2789
+ tol : float, optional
2790
+ The absolute tolerance for comparing values.
2791
+ Default is :attr:`TOL.absolute`.
2792
+
2793
+ Returns
2794
+ -------
2795
+ bool
2796
+ True if all corresponding values of the two lists are closer than the tolerance.
2797
+ False otherwise.
2798
+
2799
+ Warnings
2800
+ --------
2801
+ .. deprecated:: 2.0
2802
+ Will be removed in 2.1
2803
+ Use :func:`TOL.is_close` instead.
2804
+
2805
+ The tolerance value used by this function is an absolute tolerance.
2806
+ It is more accurate to use a combination of absolute and relative tolerance.
2807
+ Therefor, use :func:`TOL.is_allclose` instead.
2808
+
2809
+ Notes
2810
+ -----
2811
+ The function is similar to NumPy's *allclose* function [1]_.
2812
+
2813
+ References
2814
+ ----------
2815
+ .. [1] https://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html
2816
+
2817
+ Examples
2818
+ --------
2819
+ >>> allclose([0.1, 0.2, 0.3, 0.4], [0.1, 0.20001, 0.3, 0.4])
2820
+ True
2821
+
2822
+ >>> allclose([0.1, 0.2, 0.3, 0.4], [0.1, 0.20001, 0.3, 0.4], tol=1e-6)
2823
+ False
2824
+
2825
+ """
2826
+ return TOL.is_allclose(l1, l2, atol=tol)