The cross-platform Firemonkey framework of Delphi introduced 3D support which is based on both OpenGL and DirectX. Although it’s not as rich as GLScene (an open source Delphi 3D Components Library), for me it is a better way to deal with 3D if you are making a business application. Because it is simpler and has got a more traceable code path to review. I have used GLScene in some of my previous projects but I liked Firemonkey 3D more. Maybe this is about I am not a game programmer and only use 3D for CAD and business applications but if I would write a game application I would again prefer Firemonkey 3D instead of GLScene VCL. Anyway, if you also like Firemonkey 3D as me, you also have a problem. Firemonkey doesn’t have enough components to make a high level 3D applications. But who cares? You have a lot of ways to make your own 3D components, and I am sure in near feature there will be a lot of programmers who will publish rich components for Firemonkey on their blogs. In one of my projects I needed a pipe object, which includes an inner cylinder, and outer cylinder and two annulus on top and bottom. I could use standard cylinder object but it would not be enough for me. First reason for this was, the top and bottom would remain as circles but I needed annulus shape, secondly when I want to modify the meshes I would need my own mesh math. So I decided to make a brand new TPipe (and also TAnnulus) object, which you can also modify it with bend, twist and emboss modifiers. I liked what I have at the end, I don’t say it is the best pipe mesh on the net, but I am sure you will not have a better one until near feature.
What is a Pipe?
A pipe in geometry is a real-world shape like cylinder. An absolute cylinder shape has not got a thickness, if you put a thinner cylinder inside a thicker one, you will simply get a pipe with thickness of radius difference. As you may have thought this object will have got special top/bottom closes. It will not be a circular disk but it will be a an annulus shape.
So a pipe is in fact an extrude of an annulus. It seems there is an easy way to have a pipe in Firemonkey. Make an annulus shaped polygon, and extrude it with proper calls. Is that easy?
How to make a Pipe in Firemonkey.
The answer is no. It is not as easy as extruding an annulus because you can’t modify the vertices of an extruded shape. For example if you want to bend your pipe to a specific angle, then you should modify the vertices. So in Firemonkey the best approach is using a meshed object which the TCube, TCylinder, etc. also use. Firemonkey has got a TCustomMesh class which you can inherit any new meshed object. Building meshes is very easy if you know the math behind it. So in my TAnnulus and TPipe I have used the TCustomMesh class as the base class, rebuild the mesh according to my own geometry and Firemonkey has done the rest.
The pipe is laid through the Y axis. So the Width and Depth defines the a,b diameters of the elliptical annulus (it is a circular when Width and Depth is equal). The Height value defines the length of the cylindrical side on Y axis. The inner circle (or ellipse) is calculated from Width and Depth parameters using the Thickness property.
Having a Rectangular Shape
A pipe is a cylinder-like object so an annulus is circle-like one. But why don’t we try to think the overall shape as rectangular. The math is not so different so I also added the rectangular shape to my annulus and pipe.You can define if the inner, outer or both frames as rectangular or elliptical.
Having a Section On Pipe
Sometimes we may need to see the inside of a pipe by making angular sections on it. I have handled this case by adding a section feature in my pipe. You can define the section in any magnitude using the SectionAngle property and you can locate the section on top or bottom side using the SectionType property. (sctNone, sctTop, sctBottom). The section is on X,Y plane.
The Pipe Modifiers
A pipe modifier is a specific class (TPipeModifier) which takes the circular (or rectangular) X,Z sections of the pipe a TPointLayer, and modifies them according to its own math. For instance if you want to make a thicker emboss on a specific place of pipe, then the modifier will scale the layers. If you want to bend the modifier will rotate the layers. All modifying is based on the X,Z section layers. A pipe modifier will need a start position and an end position. All positions are based on 0, which means the bottom start of the pipe, on its local Y = -Height/2. The modifiers also have got a Subdivisions property. Using this property the modification density will be arranged.
The Bend Modifier
This TBendModifier class is used to bend the pipe starting on a specific position to a specific position using a specific angle and subdivisions. If the total length of the bend (EndPosition – StartPosition) is bigger the bend radius will be bigger. The bend is arranged with BendAngle. When following the / axis for bottom to top, a positive bend will mean a left side bend, while a negative bend will mean a right side bend. If you want the bend move also on Z axis you should use the TurnAngle property.
In the above figure (1) the pipe has a length value 8. The bend starts on 1.0 and ends on 2.5. The bend angle is 90 degrees so it is on the left. In the below figure (2) a second bend is added on the position 2.5. It ends on the position 4.It’s angle is -90, so it is a turn to right.
Now if you want to turn the bend on Z axis we will use the TurnAngle property. In the figure below (3) the first bend has got an extra TurnAngle value of 90 with its 90 degrees bend angle, and the second is same.
The Emboss Modifier
Using the emboss modifier, you can make any part of the pipe bigger or smaller. Starting from the StartPosition and ending on EndPosition the section of the pipe is scaled with the thickness ratio of the modifier. The thickness ration is 0.1 by default. To make a smaller section use a negative value. The margins are 0.02 by default, but if you want softer ends you can use a little bigger value like 0.1 or 0.2.
The Twist Modifier
This a bonus modifier. 🙂 ) . Normally it is not a commonly used effect in business applications but you can use it to see what you can do with modifiers.
For downloading the full source code of the demo project with the FMX.MeshObjects unit which includes the TAnnulus, TPipe and modifiers, you can use this SVN link. For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) applications are also available to download.