Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Schnodahipfe · Jan 11, 2014 at 03:08 PM · vector3directionlinefit

Fit line through cluster of points

I have a certain number of points/vectors that approximately lie on a line in 3D space. Are there any existing functions in unity that can fit a line through a cluster of 3D points/vectors?

The problem can be solved mathematically with a orthogonal linear regression (amongst others), that minimizes the squared error. However, it's been ages since I've worked with stuff like principal component analysis, eigenvectors, eigenvalues and so on, so implementing it myself in C# would be an enourmous hustle. Unfortunately I haven't found any C# implementation either.

A solution I'm looking for doesn't have to be as mathematically optimal as the mentioned regression, I simply want to get a rough mean direction vector from my points.

I thought about fitting a cylinder or a quad around the point cluster and using the line that goes through the centers of the capping circles/squares. Does anyone have an idea how to go about doing that script wise?

Thanks in advance!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Schnodahipfe · Jan 11, 2014 at 04:39 PM 0
Share

If anyone wants to give it a try and implement the regression in C# ;) http://en.wikipedia.org/wiki/User:Vossman/3D_Line_Regression

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Jamora · Jan 11, 2014 at 05:32 PM

A very simple way to get an extremely rough plot is to take the average of all points and plot a line from the average of a few points form the beginning to the average of all points. This of course requires you to have sorted your points somehow, e.g. first by x, then y and finally z. Radix sort is good for this purpose. If you believe in outliers, be warned that this method is very sensitive to them in your data set, as they will strongly skew the regression.

     public Vector3 GetRoughDirection(Vector3[] dataSet){
         Vector3[] sortedSet = SortXYZ(dataSet);
         Vector3 averagePoint = Average(dataSet);
         Vector3 beginning = Average(new Vector3[]{sortedSet[0],sortedSet[1],sortedSet[2]});
         return averagePoint-beginning;
     }
 
     private Vector3 Average(Vector3[] dataSet){ 
         float sumX = 0f;
         float sumY = 0f;
         float sumZ = 0f;
         foreach(Vector3 v in dataSet){
             sumX += v.x;
             sumY += v.y;
             sumZ += v.z;
         }
         return new Vector3(sumX/dataSet.Length, sumY/dataSet.Length, sumZ/dataSet.Length);
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by HappyMoo · Jan 11, 2014 at 04:01 PM

Hi, no, fitting a cylinder requires the same kind of math as fitting the line in the first place...

It's been ages for me too since I've done this kind of stuff, but it doesn't look hard at all...

Check this for the 2D version: http://faculty.cs.niu.edu/~hutchins/csci230/best-fit.htm

now for 3D you can basically just do the 2D Algorithm two times... first by only using x/y and then by looking from above and using x/z instead, so you get all components to build your 3D line

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HappyMoo · Jan 11, 2014 at 04:02 PM 0
Share

Let me know if you need more help, but you'll probably be able to figure this out

avatar image Schnodahipfe · Jan 11, 2014 at 04:38 PM 0
Share

Thanks, good idea, I'll try that. However, this definitely results in a different line than the 3D version would. I'll post my solution as soon as I have one :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform angle to grid position 2 Answers

Make a moving object continue it's movement from the same direction , even it reaches target 1 Answer

How do I turn 1 objects rotation into another objects movement direction? 1 Answer

Finding difference between gameObject's x coordinate according to one object's transform 1 Answer

Sensitivity on Android Input.acceleration 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges