Creating a 3d object

Blitz3D Forums/Blitz3D Programming/Creating a 3d object

Hi heres what I would like to do.

From a 2-d line create a 3d cylindrical object which edges conform to the contour of the 2d line. I would use readpixel from the image and then move the vertexs to match the ofset from the centre line of the 2-d image.

The reason i want to do this is that i am developing software to control a metal lathe that is hooked up with stepper motors and would like to design the object to be cut in a 2d drawing app and then produce a 3-d image on the computer screen that represents the final designed object.

Hope somebody understands what I mean.

Any sample code would be greatly appreiciated that demostrates a method that I could use to acheive this.

Thanks in advance.

Something like this, perhaps?
Const CYLINDER_QUALITY = 32

i = LoadImage(Input$("image filename: "))
If i Then
	w = ImageWidth(i)
	h = ImageHeight(i)
Else
	w = 20
	h = 20
EndIf

Dim cylinder_sample#(w-1)

For x = 0 To w-1
	cylinder_sample(x) = h-1 ; default
	If i Then
		For y = 0 To h-1
			If ReadPixel(x, y, ImageBuffer(i)) > 0 Then
				cylinder_sample(x) = Float(y) / (h-1)
				Exit
			EndIf
		Next
	Else
		cylinder_sample(x) = (Sin(x*15)*20)/(h-1) ; DEBUG
	EndIf
Next

Graphics3D 800, 600
light = CreateLight()
TurnEntity(light, 45, 30, 0)
camera_pivot = CreatePivot()
camera = CreateCamera(camera_pivot)
PositionEntity(camera, 0, 0, -20)


For x = 0 To w-1
	segment = CreateCylinder(CYLINDER_QUALITY)
	d = 1 + cylinder_sample(x) * 10
	ScaleEntity(segment, d/2.0, 0.5, d/2.0)
	TurnEntity(segment, 0, 0, 90)
	PositionEntity(segment, x - w/2, 0, 0)
Next

While Not KeyHit(1)
	TurnEntity(camera_pivot, 0, .6, 0)
	RenderWorld
	Flip
Wend
End


Or constructing a mesh instead of using primatives:
Const CYLINDER_QUALITY = 32

; load image
i = LoadImage(Input$("image filename: "))
If i Then
	w = ImageWidth(i)
	h = ImageHeight(i)
Else
	w = 20
	h = 20
EndIf

; read samples
Dim cylinder_sample#(w-1)
For x = 0 To w-1
	If i Then
		; look down until we find a non-black pixel
		cylinder_sample(x) = h-1 ; default value for if all pixels are black
		For y = 0 To h-1
			If ReadPixel(x, y, ImageBuffer(i)) > 0 Then
				cylinder_sample(x) = Float(y) / (h-1)
				Exit
			EndIf
		Next
	Else
		cylinder_sample(x) = Abs(Sin(x*15)*20)/(h-1)
	EndIf
Next

; init 3d
Graphics3D 800, 600
AmbientLight(30, 30, 30)
SeedRnd(MilliSecs())
light = CreateLight()
TurnEntity(light, 30, 60, 0)
camera_pivot = CreatePivot()
camera = CreateCamera(camera_pivot)
PositionEntity(camera, 0, 0, -20)

; init our object
mesh = CreateMesh()
surf = CreateSurface(mesh)

; plot vertices in a circle around each sampling point along the X axis
For x = 0 To w-1
	d = 1 + cylinder_sample(x) * 5
	For v = 0 To CYLINDER_QUALITY-1
		ang# = v * (360.0 / CYLINDER_QUALITY)
		vi = AddVertex(surf, x, Cos(ang)*d, Sin(ang)*d)
	Next
Next

; connect the dots with quads
For x = 1 To w-1
	For v = 1 To CYLINDER_QUALITY-1
		vi = x*CYLINDER_QUALITY + v
		AddTriangle(surf, vi-CYLINDER_QUALITY, vi, vi-CYLINDER_QUALITY-1)
		AddTriangle(surf, vi, vi-1, vi-CYLINDER_QUALITY-1)
	Next
	; slightly different math for the last quad
	v0 = x*CYLINDER_QUALITY + 0
	AddTriangle(surf, v0-CYLINDER_QUALITY, v0, v0-1)
	AddTriangle(surf, v0, v0+CYLINDER_QUALITY-1, v0-1)
Next

; put caps on both sides with triangles
v0 = 0*CYLINDER_QUALITY + 0
vN = (w-1)*CYLINDER_QUALITY + 0
For v = 2 To CYLINDER_QUALITY-1
	AddTriangle(surf, v0, v0+v, v0+v-1)
	AddTriangle(surf, vN, vN+v-1, vN+v)
Next

; fix normals
UpdateNormals(mesh)

; centre the mesh
PositionEntity(mesh, -w/2, 0, 0)

; show it
While Not KeyHit(1)
	TurnEntity(camera_pivot, 0, .6, 0)
	RenderWorld
	Flip
Wend
End


I guess you mean construction of a 3d solid around a centre of rotation from a series of 2d line segments or perhaps splines extrapolated from an outline 2d image. You then specify the centre of rotation and for simplicity probably rotate the image so that this axis is vertical or horizontal.

I am thinking you would probably want to use splines to smooth out the pixel outline and get nice curves. This should perhaps be optional though as perhaps you may not always want curves or maybe have a mixture of both, so you should be able to modify and edit the 2d data that has been generated from the image accordingly.

If you are going to do that though you might as well write the drawing part of it youself and avoid image interpretation. If you are drawing this thing as a vector object you will loose a lot of accuracy by converting it to an image and so you will have to edit the resulting data in a lot of cases to get the exact form you are after.

With the splines you could vary the number of vertices or control points depending on their curvature and on output destination. You would probably want to send a lot of control data to the lathe, but would require somewhat less for the 3d visualisation. I guess you would do something similar for all the line segments.

Exactly how to code this is a little above my head though, but it sounds like an interesting project.

Thanks for the code Octothorpe, I will give the mesh version a go and let you know how it works out in my app.

Yeah DJ thats it, youve got the idea of exactly what I am trying to achieve. The lathe has an acuracy of .1mm so i will need to interpolorate the 2d image to acheive the required resolution.

The 3d image doesnt need to be accurate to the same degree, it is mearly a graphical representation of the final object that will be made.

I intend to allow the x scale in the 2d editor to be specified to allow very detailed items to be lathed if required.

I have already cut 1/92 scale cannons using a pic processor to store the data and have acheived great succes so the next step is to get an all singing and dancing editor done to allow full pc control.

Thanks alot for the help and info so far guys.

A linear interpolation of 2D points is going to create straight sections in your model. You'd need a way to derive a curve from several points (which I don't know how to do).

How are you going to model a curve with triangles? You're going to have to sample the data sooner or later.