Click here to Skip to main content
15,867,568 members
Articles / Multimedia / GDI+

3D Geometry Library (Basic Classes) and 3D Drawing using VB.NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (21 votes)
7 Sep 2007CPOL3 min read 127.3K   8.7K   62   13
This article explains a 3D geometry library developed using VB.NET.

Introduction

I had a requirement to plot a list of 3D points on a graphics display. The list of points is generated by some other software. I had to do this using .NET. I am not using DirectX or any other library. I have created a 3D geometry library with just the basic classes like Vector, Matrix, and Point. A test application also has been developed to test this library. The test application reads a point data file and draws lines connecting the read points. It also has options to view the 3D object along different axes. I have done these using VB.NET 2.0.

Geometry Library (Basic Classes)

This geometry library contains only some basic classes like Vector, Matrix, and Point. I have created 2D and 3D classes for each of these (Vector2D, Vector3D, Matrix2D, Matrix3D, Point2D and Point3D). The 2D classes are used for two-dimensional geometry, and 3D classes for three-dimensional geometry. You can get more detailed explanations about Geometry, Vector and Matrix functionalities by searching on the web.

Point

A Point represents a two dimensional or three dimensional point in space. A Point is represented by its X and Y (also Z in the case of a 3D point) coordinates.

Vector

A geometrical Vector specifies the direction in space. The Vector has two properties: Length and Direction. The Vector does not have a fixed location in space.

Our Vector classes have standard vector operations like Addition, Subtraction, Transformation, Negation, Dot Product, Cross Product, finding the angle between two Vectors etc.

Matrix

Matrices allow arbitrary linear transformations to be represented in a consistent format, suitable for computation. This also allows transformations to be concatenated easily by multiplying their matrices - from Wikipedia. We use a 3x3 matrix for 2D and 4x4 matrix for 3D. You can find more information by searching on the web.

I have created specific Matrix classes for 2D and 3D for convenience. Alternatively, a generic Matrix class can be created and 2D and 3D Matrix classes can be derived from the generic class.

Our Matrix classes have standard matrix operations like Multiplication, Inverse, Rotation, Scaling, and Transformation etc.

Using the 3D matrix class:

VB
' create a new instance, which creates a identity matrix 
Dim Mat As New Matrix3D 
' set the matrix for rotation, 90 degrees about X axis 
Mat.SetToRotation(90 * Math.PI / 180.0, New Vector3D(1, 0, 0)) 
' use the matrix to transform the points 
TransformPoints(Mat) 
' create a new point 
Dim Pt As New Point3D(50.0, 50.0, 0.0) 
' transform the point with the matrix 
Pt.TransformBy(Mat) 
' resulting point will be : (50.0, 0.0, -50.0)

Test Application

The test application reads a point data file and draws lines connecting the list of points. The file contains a list of 3D points. The X, Y, and Z coordinates are separated by spaces, and each point is stored in new lines.

0.0 0.0 0.0 
0.0 0.0 50.0 
50.0 0.0 50.0 

The functions PointReader and ReadPointFromString parse the data file and fill the points array. In this example data file, the points are manipulated to get the desired result. But the original requirement was to plot the points on the screen. But for demonstrating the geometry library, I have considered drawing lines, to show a 3D shape object.

Looking at Various Views

Functionalities of the test application include viewing the 3D object from X-axis, Y-axis, Z-axis, and 3D view.

Screenshot - 3DView.jpg

3D View

Screenshot - ViewX.jpg

View from X

Screenshot - ViewY.jpg

View from Y

Screenshot - ViewZ.jpg

View from Z

For example, to get the view from the X direction, first we transform the points to the original position, then create the required Matrix object and transform the points to the required position.

VB
' set the drawing to initial position (looking from Z) 
Dim Mat As New Matrix3D 
' create a new matrix object and set that to the 
' inverse of the existing matrix Mat = TransMat.GetInverse 
' transform the points to the initial position 
TransformPoints(Mat)
TransMat.PostMultiplyBY(Mat) 
Mat.SetToIdentity() 
' create a matrix with the required rotation 
Mat.SetToRotation(90 * Math.PI / 180.0, New Vector3D(0, 1, 0)) 
' and transorm the points TransformPoints(Mat) 
' update the matrix data member 
TransMat.PostMultiplyBY(Mat) 
' refresh the drwing area 
PanelDraw.Invalidate()

I am using the Panel control for drawing the graphics, using the double buffer concept to avoid flickering, when the graphics refreshes (http://www.thescripts.com/forum/thread267635.html).

Hope this article is of some use. There may be better ways to achieve the requirement. Please let me know your views and clarifications if any.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Have good experience in AutoCAD and around 6 years software development experience using ObjectARX (AutoCAD API), C++, VB and .Net technologies.

Comments and Discussions

 
Answerfixed an error Pin
Southmountain30-Apr-22 12:04
Southmountain30-Apr-22 12:04 
Questiondear developer Pin
shahabuddin rana25-Jan-21 3:57
shahabuddin rana25-Jan-21 3:57 
QuestionMultiply Multiple Matrix Pin
Member 1098211627-Jun-16 12:44
Member 1098211627-Jun-16 12:44 
GeneralMy vote of 5 Pin
Eduardo_David30-Oct-12 2:38
Eduardo_David30-Oct-12 2:38 
QuestionCreate a geometry from point cloud Pin
ChadSixt29-Dec-11 5:50
ChadSixt29-Dec-11 5:50 
Questionplzzzzzzz Pin
ishan2ma2-Dec-11 5:54
ishan2ma2-Dec-11 5:54 
GeneralMy vote of 5 Pin
Paolo65(1)18-Nov-10 0:50
Paolo65(1)18-Nov-10 0:50 
GeneralHello Pin
mrlaunched9-Mar-10 2:58
mrlaunched9-Mar-10 2:58 
GeneralProblem running program Pin
John_Mac31-May-09 10:02
John_Mac31-May-09 10:02 
Questionimport geometry library Pin
niknurayunni6-Sep-08 17:57
niknurayunni6-Sep-08 17:57 
GeneralGOOD Article! Pin
hungud1-Aug-08 22:14
hungud1-Aug-08 22:14 
QuestionLinking Autocad with vb.net Pin
Member 413048814-Oct-08 10:20
Member 413048814-Oct-08 10:20 
GeneralVery nice! Pin
andalmeida8-Sep-07 10:07
andalmeida8-Sep-07 10:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.