Module: TT::Geom3d

Defined in:
TT_Lib2/geom3d.rb,
cext/tt_lib2/tt_lib2/src/ruby/geom3d/geom3d.cpp

Overview

Since:

  • 2.0.0

Class Method Summary collapse

Class Method Details

.arc(center, xaxis, normal, radius, start_angle, end_angle, num_segments = 12) ⇒ Array<Geom::Point3d>

Creates a set of Geom::Point3d objects for an arc.

Parameters:

  • center (Geom::Point3d)
  • xaxis (Geom::Vector3d)
  • normal (Geom::Vector3d)
  • radius (Number)
  • start_angle (Float)

    in radians

  • end_angle (Float)

    in radians

  • num_segments (Integer) (defaults to: 12)

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.0.0



57
58
59
60
61
62
63
64
65
66
67
68
# File 'TT_Lib2/geom3d.rb', line 57

def self.arc(center, xaxis, normal, radius, start_angle, end_angle, num_segments = 12)
	# Generate the first point.
	t = Geom::Transformation.rotation(center, normal, start_angle )
	points = []
	points << center.offset(xaxis, radius).transform(t)
	# Prepare a transformation we can repeat on the last entry in point to complete the arc.
	t = Geom::Transformation.rotation(center, normal, (end_angle - start_angle) / num_segments )
	1.upto(num_segments) { |i|
		points << points.last.transform(t)
	}
	return points
end

.arc2d(center, xaxis, radius, start_angle, end_angle, segments = 24) ⇒ Array<Geom::Point3d>

Parameters:

  • center (Geom::Point3d)
  • xaxis (Geom::Vector3d)
  • radius (Numeric)
  • start_angle (Numeric)
  • end_angle (Numeric)
  • segments (Integer) (defaults to: 24)

Returns:

  • (Array<Geom::Point3d>)

See Also:

Since:

  • 2.7.0



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'TT_Lib2/geom3d.rb', line 83

def self.arc2d( center, xaxis, radius, start_angle, end_angle, segments = 24 )
  full_angle = end_angle - start_angle
  segment_angle = full_angle / segments
  t = Geom::Transformation.axes( center, xaxis, xaxis * Z_AXIS, Z_AXIS )
  arc = []
  (0..segments).each { |i|
    angle = start_angle + (segment_angle * i)
    x = radius * Math.cos(angle)
    y = radius * Math.sin(angle)
    arc << Geom::Point3d.new(x,y,0).transform!(t)
  }
  arc
end

.arc_segments(angle, full_circle_segments, force_even = false) ⇒ Integer

Calculates the number of segments in an arc given the segments of a full circle. This will give a close visual quality of the arcs and circles.

Parameters:

  • angle (Float)

    in radians

  • full_circle_segments (Integer)
  • force_even (Boolean) (defaults to: false)

    useful to ensure the segmented arc's apex hits the apex of the real arc

Returns:

  • (Integer)

Since:

  • 2.0.0



138
139
140
141
142
143
# File 'TT_Lib2/geom3d.rb', line 138

def self.arc_segments(angle, full_circle_segments, force_even = false)
	segments = (full_circle_segments * (angle.abs / (Math::PI * 2))).to_i
	segments += 1 if force_even && segments % 2 > 0 # if odd
   segments = 1 if segments < 1
	return segments
end

.average_point(points) ⇒ Geom::Point3d

Parameters:

  • points (Array<Geom::Point3d>)

Returns:

  • (Geom::Point3d)

Since:

  • 2.5.0



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'TT_Lib2/geom3d.rb', line 194

def self.average_point(points)
   average = Geom::Point3d.new(0,0,0)
   return average if points.empty?
	number_of_points = points.length
   for pt in points
     average.x += pt.x
     average.y += pt.y
     average.z += pt.z
   end
   average.x = average.x / number_of_points
   average.y = average.y / number_of_points
   average.z = average.z / number_of_points
   average
end

.circle(center, normal, radius, num_segments) ⇒ Array<Geom::Point3d>

Creates a set of Geom::Point3d objects for an circle.

Parameters:

  • center (Geom::Point3d)
  • normal (Geom::Vector3d)
  • radius (Number)
  • num_segments (Integer)

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.0.0



107
108
109
110
111
# File 'TT_Lib2/geom3d.rb', line 107

def self.circle(center, normal, radius, num_segments)
	points = self.arc(center, normal.axes.x, normal, radius, 0.0, Math::PI * 2, num_segments)
	points.pop
	return points
end

.circle2d(center, xaxis, radius, segments = 24) ⇒ Array<Geom::Point3d>

Parameters:

  • center (Geom::Point3d)
  • xaxis (Geom::Vector3d)
  • radius (Numeric)
  • segments (Integer) (defaults to: 24)

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.7.0



121
122
123
124
125
# File 'TT_Lib2/geom3d.rb', line 121

def self.circle2d( center, xaxis, radius, segments = 24 )
  segments = segments.to_i
  angle = 360.degrees - ( 360.degrees / segments )
  self.arc2d( center, xaxis, radius, 0, angle, segments - 1 )
end

.interpolate_linear(point1, point2, subdivs) ⇒ Array<Geom::Point3d>

Parameters:

  • point1 (Geom::Point3d)
  • point2 (Geom::Point3d)
  • subdivs (Integer)

    The number of resulting segments

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.5.0



179
180
181
182
183
184
185
186
187
# File 'TT_Lib2/geom3d.rb', line 179

def self.interpolate_linear(point1, point2, subdivs)
	step = 1.0 / subdivs
   pts = []
   (0..subdivs).each { |i|
     r = step * i
     pts << Geom::linear_combination( r, point1, 1.0 - r, point2 )
   }
   pts
end

.normalize_plane(plane) ⇒ Array<Geom::Point3d, Geom::Vector3d>

Returns plane in the format [ point3d, vector3d ].

Parameters:

  • plane (Array<Geom::Point3d, Geom::Vector3d>, Array<Number, Number, Number, Number>)

Returns:

  • (Array<Geom::Point3d, Geom::Vector3d>)

Since:

  • 2.0.0



20
21
22
23
24
25
26
# File 'TT_Lib2/geom3d.rb', line 20

def self.normalize_plane(plane)
	return plane if plane.length == 2
	a, b, c, d = plane
	v = Geom::Vector3d.new(a,b,c)
	p = ORIGIN.offset(v.reverse, d)
	return [p, v]
end

.planar_points?(points) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



36
37
38
39
40
41
42
# File 'TT_Lib2/geom3d.rb', line 36

def self.planar_points?(points)
   points = TT::Point3d.extend_all( points )
   points.uniq!
   return false if points.size < 3
   plane = Geom.fit_plane_to_points( points )
   points.all? { |pt| pt.on_plane?( plane ) }
end

.spiral_sphere(number_of_points, origin = ORIGIN) ⇒ Array<Geom::Point3d>

Parameters:

  • number_of_points (Integer)
  • origin (Geom::Point3d) (defaults to: ORIGIN)

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 2.3.0



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'TT_Lib2/geom3d.rb', line 154

def self.spiral_sphere( number_of_points, origin=ORIGIN )
  t = Geom::Transformation.new( origin )
  n = number_of_points
  node = Array.new( n )
  dlong = Math::PI * (3-Math.sqrt(5))
  dz = 2.0/n
  long = 0
  z = 1 - dz/2
  (0...n).each { |k|
    r = Math.sqrt( 1-z*z )
    pt = Geom::Point3d.new( Math.cos(long)*r, Math.sin(long)*r, z )
    node[k] = pt.transform( t )
    z = z - dz
    long = long + dlong
  }
  node
end