5,666,979 members and growing! (15,994 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

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

By _Bala

This article explains a 3D Geometry library developed using VB.Net
VB 8.0, VB, Windows, .NET, .NET 2.0, GDI+, VS2005, Visual Studio, Dev

Posted: 7 Sep 2007
Updated: 7 Sep 2007
Views: 17,511
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.71 Rating: 3.56 out of 5
2 votes, 18.2%
1
0 votes, 0.0%
2
1 vote, 9.1%
3
1 vote, 9.1%
4
7 votes, 63.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

I got 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 Dot Net. I am not using DirectX or any other library. I have created a 3D Geometry library with just 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 two classes 2D and 3D for each of these. (Vector2D, Vector3D, Matrix2D, Matrix3D, Poin2D 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 2 dimensional or 3 dimensional point in space. A Point is represented by its X and Y (also Z in case of 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, Get the inverse of the matrix, Get the rotation, scaling and transformation matrices etc.

Using the 3D matrix class:

' 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 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.

' 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

_Bala


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

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Questionimport geometry librarymemberniknurayunni18:57 6 Sep '08  
GeneralGOOD Article!memberhungud23:14 1 Aug '08  
QuestionLinking Autocad with vb.netmemberMember 413048811:20 14 Oct '08  
GeneralVery nice!memberandalmeida11:07 8 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Sep 2007
Editor:
Copyright 2007 by _Bala
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project