Should I compare 16 elments of transformations to determine if the 2 transformations are actually the same? Or any simpler way to do it?
Using the Transformation to_a-instance_method and compare the arrays is can be one way.
You can check also Transformation Inspector | SketchUp Extension Warehouse in the file:
how Thomas did it with his
#same? method, using SketchUp’s internal precision for Point3d and Vector3d comparison.
Whatever approach you take, you need to allow for computer arithmetic limitations.
The contents of a Transformation are usually generated by calculations, particularly if rotations or scaling are involved. Computer calculations are subject to imprecision. So, the only situation you can expect exact equality is if the Transformation was copied from an existing one. But, of course, then you already know the two are the same! In all other situations you must compare to within a tolerance.
There is nothing special to Transformations about this; it is just necessary programming when floating point values are involved.
My attempt …
# Build a hash of transformation properties. # @param t [Geom::Transformation] # @return [Hash] def transform_properties(t) vecX = X_AXIS.transform(t) vecY = Y_AXIS.transform(t) vecZ = Z_AXIS.transform(t) Hash[ :origin, t.origin, # Geom::Point3d :xaxis, t.xaxis, # Geom::Vector3d (normalized) :yaxis, t.yaxis, # Geom::Vector3d (normalized) :zaxis, t.zaxis, # Geom::Vector3d (normalized) :xscale, (vecX.length*vecX.normalize.x).to_l, # Length :yscale, (vecY.length*vecY.normalize.y).to_l, # Length :zscale, (vecZ.length*vecZ.normalize.z).to_l, # Length ] end
# Compare two hashes of transformation properties. # Hash#== will use each value's class #== method (in # addition to comparing keys and number of members.) # @param t1 [Geom::Transformation] # @param t2 [Geom::Transformation] # @return [Boolean] def transforms_equal?(t1,t2) transform_properties(t1) == transform_properties(t2) end
This is my usual approach. This works from SU 2018 and later, where
Geom::Transformation#identity? was fixed.
(transformation1 * transformation2.inverse).identity?