Who can help with a script to create a number of points around a sphere?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
freecad-heini-1
Veteran
Posts: 7788
Joined: Tue Jan 07, 2014 11:10 am
Contact:

Who can help with a script to create a number of points around a sphere?

Post by freecad-heini-1 »

Hello,
who can help with a script or a existing tool to create a number of points around a sphere?
Important is, that all points should have the same distance to each other.

Type in the sphere diameter and the number of points, and the script should do the job.
https://forum.freecadweb.org/viewtopic. ... 08#p269208

Thank you so much and best regards
Wilfried
oddtopus
Posts: 142
Joined: Tue Sep 20, 2016 6:17 pm

Re: Who can help with a script to create a number of points around a sphere?

Post by oddtopus »

Hi. I'm not sure that it's possible what you are asking.
If I remember well there are just 5 regular solids possible in 3d.
Try "tesselation of sphere" in your favorite search engine.
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Who can help with a script to create a number of points around a sphere?

Post by ulrich1a »

Why dont you use the Geodesic macro. It is also available via the Add-On Tool. You can Input the Diameter and get a dome with a number of vertexes. Its structure is very regular. The vertexes are lying on a sphere with the given Diameter.

There is the frequency parameter. Here is a table with the frequency Parameter and the resulting vertexes:

Frequency Vertexes
4 162
5 252
6 362

Ulrich
User avatar
PrzemoF
Veteran
Posts: 3520
Joined: Fri Jul 25, 2014 4:52 pm
Contact:

Re: Who can help with a script to create a number of points around a sphere?

Post by PrzemoF »

Some food for thoughts [1][2]. In [1] there are a few pieces of code that can be easily converted with "bolting-on" FreeCAD code to create spheres.

Code: Select all

# -*- coding: utf-8 -*-

# Macro Begin: /home/przemo/.FreeCAD/sp1.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import Part
import math
import random

def fibonacci_sphere(samples=1,randomize=True):
    rnd = 1.
    if randomize:
       rnd = random.random() * samples

    points = []
    offset = 2./samples
    increment = math.pi * (3. - math.sqrt(5.));

    for i in range(samples):
        y = ((i * offset) - 1) + (offset / 2);
        r = math.sqrt(1 - pow(y,2))

        phi = (i % samples) * increment

        x = math.cos(phi) * r
        z = math.sin(phi) * r

        points.append([x,y,z])

    return points

App.ActiveDocument.addObject("Part::Sphere",'base')
App.ActiveDocument.ActiveObject.Label = 'base'
FreeCAD.getDocument("Unnamed").getObject('base').Radius = '1 mm'

p = fibonacci_sphere(samples=120, randomize=False)
i =0

for s in p:
	name = 'sp_{}'.format(i)
	App.ActiveDocument.addObject("Part::Sphere",name)
	App.ActiveDocument.ActiveObject.Label = name
	FreeCAD.getDocument("Unnamed").getObject(name).Placement = App.Placement(App.Vector(s[0],s[1],s[2]),App.Rotation(App.Vector(0,0,1),0))
	FreeCAD.getDocument("Unnamed").getObject(name).Radius = '0.2 mm'
	i += 1

App.ActiveDocument.recompute()
Gui.SendMsgToActiveView("ViewFit")
The result attached.

[2] could be easily implemented by simple 'physics' engine:
- throw points in random locations
- calculate forces (Coulomb's law)
- move the point in force direction by a (small) distance depending on the value of force
- recalculate and check is locations are "stable"

[1] https://stackoverflow.com/questions/960 ... 68#9606368
[2] https://www.mathpages.com/home/kmath005/kmath005.htm
Attachments
Zrzut ekranu z 2018-11-19 22-06-25.png
Zrzut ekranu z 2018-11-19 22-06-25.png (342.78 KiB) Viewed 1049 times
freecad-heini-1
Veteran
Posts: 7788
Joined: Tue Jan 07, 2014 11:10 am
Contact:

Re: Who can help with a script to create a number of points around a sphere?

Post by freecad-heini-1 »

ulrich1a wrote: Mon Nov 19, 2018 8:39 pm Why dont you use the Geodesic macro. It is also available via the Add-On Tool. You can Input the Diameter and get a dome with a number of vertexes. Its structure is very regular. The vertexes are lying on a sphere with the given Diameter.

There is the frequency parameter. Here is a table with the frequency Parameter and the resulting vertexes:

Frequency Vertexes
4 162
5 252
6 362

Ulrich
Hi Ulrich,
thank you so much. I didn't know there was such a macro. It's great and very helpful.
Best regards
Wilfried
Post Reply