Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / how to get the normal from the triangle ?

Author
Message
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 15:32
There are three vertex from vertexdata.
x1, y1, z1
x2, y2, z2
x3, y3, z3
How to get the normal from the three vertex?
please help.

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 15:36
this is right ?

r=make vector3(1)
r=make vector3(2)
r=make vector3(3)
r=make vector3(4)
r=make vector3(5)
r=make vector3(6)

set vector3 1,x1, y1, z1
set vector3 2,x2, y2, z2
set vector3 3,x3, y3, z3
subtract vector3 5,1,2
subtract vector3 6,1,3
cross product vector3 4,5,6

.....already beside..... for all
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 21st Oct 2010 15:53
Yes, though you may need to normalize 5 and 6.

OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 16:07
like this ?
set vector3 1,x1, y1, z1
set vector3 2,x2, y2, z2
set vector3 3,x3, y3, z3


subtract vector3 5,1,2
subtract vector3 6,1,3

normalize vector3 7,5
normalize vector3 8,6

cross product vector3 4,7,8

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 16:36
and how align the object in this vector ?

x# =X VECTOR3(4)
y# =Y VECTOR3(4)
z# =Z VECTOR3(4)

x# =360.0 / x#
y# =360.0 / y#
z# =360.0 / z#

rotate object 1,x#,y#,z#

RIGHT ?

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 18:19
not work

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 18:28
EZro_FindNormal - bed work.

but how do occur in DBPro calculate what else. work here and there does not work. what's going on. all other programming languages all calculated correctly. be like? tried dozens of different ways and in each some glitches. although the C + + works with Delphi running on a DBPro does not work.

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 21:31
I didn't understand that post at all.

But your earlier post was clear.

You need to normalize vector 4 - there's no need to normalize vectors 5 and 6, i.e.

OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 21:51
thanks,
and how align the object in this vector ?

x# =X VECTOR3(4)
y# =Y VECTOR3(4)
z# =Z VECTOR3(4)
x# =360.0 / x#
y# =360.0 / y#
z# =360.0 / z#

rotate object 1,x#,y#,z#

RIGHT ?

.....already beside..... for all
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 21st Oct 2010 22:06
Try



Cheers!
Sven B

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 22:10
I don't know what you mean.

But you might mean this:


x# = X VECTOR3(4)
y# = Y VECTOR3(4)
z# = Z VECTOR3(4)

point object 1, object position x(1)+x#, object position y(1)+y#, object position z(1)+z#

OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 22:12 Edited at: 21st Oct 2010 22:14
turned .!!!!!!!!!!!!
and how it works. Why so?
Thanks.

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 22:16
You're welcome.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 22:19 Edited at: 21st Oct 2010 22:21
Tell me please if you know how to count the points belonging to the triangle in 3D coordinates.
I tried on the differences of areas not all triangles works for some reason.


.....already beside..... for all
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Oct 2010 22:21
Look into the vertexdata commands? I think? I'm not really clear on what you're asking.

OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 22:27
I say the following:
is a triangle in space, it is rotated in a random order.
is the point of collision in this triangle.
how to learn using a mathematical expression - is whether this point is inside a triangle or not.

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 22:34 Edited at: 21st Oct 2010 22:36
Ah, I see. You don't mean "count", you mean "check" or "test".

There is a standard formula for that. I think I found it some time ago by doing a Google search for something like "how to test whether a point lies inside a triangle".

Found it. Here you go:

Point in triangle test
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 22:39 Edited at: 21st Oct 2010 23:00
Quote: "Ah, I see. You don't mean "count", you mean "check" or "test"."

Yes.

I have this problem can not cope for two weeks. and where I just was not looking.
but all the options I've tried - all make some mistakes.

One of the best and that's this simple:


I tried to read as follows:
calculate the area of a large triangle.
counted the area of small triangles and their sum.
and subtracted from a large area.
introduced error result 0.0001.
and if the areas are roughly equal - means a point inside the triangle.


especially for small triangles I introduced such a test.
test of the sum of distances from point to vertices of a triangle on the least distance from all existing triangles.


.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 21st Oct 2010 23:18 Edited at: 21st Oct 2010 23:21
Quote: "Found it. Here you go:"

Yes, I found something similar but this is in 2D.
requires a projection from 3D to 2D, and that I somehow did not understand

as well as anyone can tell. Thank you

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Oct 2010 23:51 Edited at: 21st Oct 2010 23:58
Quote: "requires a projection from 3D to 2D"


Good point. Does it work if you use your normal vector to turn the edges into planes and do a similar test?

Edit The barycentric method in that link is the one you want. It should extend easily () to 3D coordinates.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 00:30 Edited at: 22nd Oct 2010 13:58
Same Side Technique is work in 2D is good



Yes, I'll try to check for your case and then write it here

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Oct 2010 12:29
Let us know if you get stuck. The main point to remember is that you need an extra scalar to allow for the fact that the point might not lie in the plane of the triangle. However, you can use the normal vector you calculated earlier for that. (It would be sensible to normalize it as well.)

If your candidate 3D point is P, the triangle's vertices are A, B and C, and the normal vector is N then the scalars you need in the barycentric method are u, v and w. These scalars satisfy the vector equation

P = A + u*(B-A) + v*(C-A) + w*N

When you write this out in scalar form you have three simultaneous equations - but these can be simplified when you remember that N is computed from the cross-product of the other two vectors, i.e. N is orthogonal to both (B-A) and (C-A). This means that if you calculate the dot product of each side with N then you get

P.N = A.N + w (because (B-A).N = (C-A).N = 0 and N.N = 1)

which gives you

w = P.N - A.N = (P-A).N

That just leaves you with two equations and two unknowns, u and v, which can be solved by elementary algebra.

Once you have w you can start your test. If w is not zero then the point is NOT in the same plane as the triangle so cannot be inside it (you'll need to allow for a small error since the calculations won't be exact) and you can stop.

If w is close enough to zero then the point is in the same plane as the triangle and it makes sense to calculate u and v as described in that link.

Hope this makes sense.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 13:36 Edited at: 22nd Oct 2010 14:19
Sorry. I have bad English and I do not understand the mathematics of vectors. Help pass it on the steps that I understand.


I have:
// point
set vector3 7,x0,y0,z0
// triangle
set vector3 1,x1,y1,z1
set vector3 2,x2,y2,z2
set vector3 3,x3,y3,z3

subtract vector3 5,1,2
subtract vector3 6,1,3
cross product vector3 4,5,6
normalize vector3 4,4
point object 5, x vector3(4) + x vector3(7), y vector3(4) + y vector3(7), z vector3(4) + z vector3(7)

What should I do next?

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 13:53 Edited at: 22nd Oct 2010 14:11
if you're interested here's what I do:

use ImageKit.dll and D3DFunc.dll




EDIT: media there ->
I want to draw directly on the object and cover it with various materials

.....already beside..... for all

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Oct 2010 16:03
Try this:

OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 19:46 Edited at: 22nd Oct 2010 20:00
triangle may be degenerate - what it means ?

EDIT: I realized that the triangle area with 0.
but I can not be of such triangles.

EDIT2:
I checked.
program says that the majority of large triangles degenerate.
while checking the area of these triangles is much greater than 1.

This code works well on the small triangle with a horizontal plane. and almost all large triangles with the vertical plane responds that they are degenerate.

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 20:56
My object is 360 triangles. I have yet to make a test of his VERTEXS.
I'll check on where they appear in vertexdata - write the result here.

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 22:23 Edited at: 22nd Oct 2010 22:41
appears in the calculations have a bug.
because this triangle can not be degenerate.

A.x =-9.98715591431
A.y =0
A.z =-3.32904601097
B.x =-9.98715782166
B.y =28.571428299
B.z =-9.98715019226
C.x =-9.98715782166
C.y =0
C.z =-9.98715019226
P.x =-9.98715209961
P.y =17.4658203125
P.z =-9.09385681152

MAKE OBJECT TRIANGLE 1, A.x,A.y,A.z,b.x,b.y,b.z,c.x,c.y,c.z



.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 22nd Oct 2010 23:32 Edited at: 22nd Oct 2010 23:38
I understand it is a problem evaluate in DBPro. YES?

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Oct 2010 23:41
Quote: "I understand it is a problem in computing DBPro. YES?"


Possibly, but in this case it's more likely to be a bug in my code - or an implicit assumption I've made. I'll check and report back as soon as I can.

Quote: "I checked.
program says that the majority of large triangles degenerate.
while checking the area of these triangles is much greater than 1.
"


Sounds like it's a problem with the error tolerance that I used. I used an absolute tolerance whereas I probably need a relative one.

Thanks for checking.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 01:08 Edited at: 23rd Oct 2010 01:11
Thank you very much for your help.
by the way, I checked the Geosphere - works!?
May go the other way and to project a triangle in 2D? (though I do not know how to do it)
I still will have to calculate the UV coordinates, and I think of 2D it will make easier.(maybe I'm wrong)

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Oct 2010 12:18
Quote: "Possibly, but in this case it's more likely to be a bug in my code - or an implicit assumption I've made."


Spot on.

I need two equations in order to find u and v. I assumed I would need either the X and Y coordinate equations or the X and Z equations when I was testing for degeneracy. Your example needs the Y and Z coordinate equations. It should be easy to fix.

I'll post the amended code later this morning (about to have a late breakfast now ).

Thanks for pointing out the bug.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Oct 2010 12:33 Edited at: 23rd Oct 2010 13:42
Try this:



Still waiting for breakfast.

Edit Had breakfast now so brain is working better.

The following revised code avoids most of the messy degeneracy checks by using standard results for solving equations by least squares:



You might still get problems with very long thin triangles but this code should work in standard situations. Let me know if you spot any additional problems.

Edit2 Forgot to add that this uses IanM's Matrix1 plug-in.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 15:33
Finally I was able to get behind the computer.
I'll bear the code. As soon as I try, I'll post.

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 16:17 Edited at: 23rd Oct 2010 16:33
!!! WoW !!!!!! WoW !!!!!! WoW
WORKS
!!! WoW !!!!!! WoW !!!!!! WoW
thank you very much again ..
I am a bit cleaner code, and now I make here.
17:13 on the clock and I can move further down the CODE . Hurrah!

EDIT:
my mother long ago said:
- Learn son while there is a possibility.
I replied:
- I'm going to work, why do this to me.
"Now "I'm sorry"

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 16:38
Works even with very small triangles at the top of the sphere (the sphere of 3600 triangles) with high accuracy!

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 17:29 Edited at: 23rd Oct 2010 17:42
found the following code to calculate the texture coordinates of a point inside the triangle.
need to think about possible applications.

Green Gandalf do not you look?


.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 22:25 Edited at: 23rd Oct 2010 22:27
seems I was able to translate into the syntax of DBP than two lines in the code.
in my opinion is the multiplication of two VECTOR2-they are in the comments. (REM .....)
I have not found in the DBP - multiplication of two VECTOR2.
please check I'm right?
original code


my code


.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 23rd Oct 2010 22:42
I decided to simplify the question

I have a line of code in C + + or?
as it moved to DBPro



where tA and tC - a Vector2
and kAEC - it FLOAT

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Oct 2010 02:42
The multiplication of two vector2's can mean one of at least three things in maths:

- the dot product, use dot product vector2 - this gives a float result
- the matrix product of a column vector2 by a row vector2, i.e. the product of a 2x1 matrix by a 1x2 matrix to give a matrix2 result (I'm not sure of the simplest way of doing this in DBPro)
- the elementwise product of two vector2s, use the DBPro command multiply vector2 to get a vector2 result.

I don't know which of these you want - the last I suspect.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 03:12 Edited at: 24th Oct 2010 03:38
Sorry. I accidentally touched the button.

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 03:12 Edited at: 24th Oct 2010 03:39
Sorry.

.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 03:35 Edited at: 24th Oct 2010 03:49
I do not seem the right thing. something I did not understand.

original code
from page
http://dtimofeev.blogspot.com/search?updated-max=2009-07-17T14%3A21%3A00%2B04%3A00&max-results=7




my code


my all program


.....already beside..... for all
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 03:58
4:55 am on the clock, I'm going to bed. head is tired.

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Oct 2010 16:59
It's no use posting a chunk of code and simply saying "something I did not understand". Tell us which part you don't understand.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 17:13
at 18:08 local time
Sorry GG. I went to sleep.
Thank you for your attention.
If you need to make a model or texture, and you will not have enough time. Treat, I am ready to help you in gratitude.

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Oct 2010 20:31
Quote: "If you need to make a model or texture, and you will not have enough time. Treat, I am ready to help you in gratitude."


Thank you! How about a model for one of the attached (without the snow)?

Attachments

Login to view attachments
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 20:47 Edited at: 24th Oct 2010 22:41
I can do both buildings.
three questions:
1. how quickly it needs to be done ?
2. a restriction on the number of triangles ?
3. on the building texture should be one, or I can separate the roof, walls, windows?



EDIT:
Green Gandalf in your code, there is a saying:
u # = (A22 # * B1 # - A12 # * B2 #) / det #
v # = (-A12 # * B1 # + A11 # * B2 #) / det #
I understand that this is the local coordinates inside the triangle from 0 to 1.
how can I U # and V # for the calculation of global UV texture coordinates. (to get the real point of UV which is checked for membership in the triangle)


EDIT2:
stone house of this type as in the photo outbuilding.jpg?
Need to do the windows on the sides that are not visible in the photo?
If so, how they are (though, would be approximately).

.....already beside..... for all
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Oct 2010 22:46
Quote: "how can I U # and V # for the calculation of global UV texture coordinates. (to get the real point of UV which is checked for membership in the triangle)"


Just use the vertex U and V values in the same way. For example, if UA, UB and UC are the U texture coordinate values for vertices A, B and C of the triangle, then the U texture coordinate for the point P inside the triangle is

UP = UA + u# * (UB-UA) + v# * (UC-UA)

The formula for V is similar.

Quote: "I can do both buildings."


Thanks.

Quote: "three questions:
1. how quickly it needs to be done ?
2. a restriction on the number of triangles ?
3. on the building texture should be one?"


1. Whenever is convenient for you.
2. Up to you - as long as they load into DBPro. Both buildings have thick stone walls: approx 0.5 metre for the house, 0.3 metre for the outbuilding.
3. Yes please - as long as the main outside walls are stone.

Let me know if you need more photos.

You can add these to your collection, I hope.
OldPMan
TGC Store Seller
15
Years of Service
User Offline
Joined: 10th Aug 2008
Location:
Posted: 24th Oct 2010 23:17 Edited at: 24th Oct 2010 23:31
WOW!!!
Thank you very much. GG you're the best!
WOW!!!

Now I can calmly accept the creation of your model.
I will definitely keep you informed as progress.

thanks again.
cheers
Andrew.

.....already beside..... for all

Login to post a reply

Server time is: 2024-04-24 22:23:08
Your offset time is: 2024-04-24 22:23:08