Skip to content

Add gravity center function #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Conversation

dura0ok
Copy link

@dura0ok dura0ok commented Jul 13, 2023

No description provided.

src/point.c Outdated
spoint_check(p);

PG_RETURN_POINTER(p);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An EOL character is needed at the end of this file.

src/point.c Outdated

for (int i = 0; i < num_elements; i++) {
current_point = array_data[i];
//elog(LOG, "%lf\n", current_point.lat);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this line of code that has been commented out.

Copy link
Contributor

@vitcpp vitcpp Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also propose to move 'int i' declaration to the function beginning. Standard C doesn't allow to declare types such the way.

src/point.c Outdated
elog(LOG, "%lf\n", v.x);
res.x+=v.x;
res.y+=v.y;
res.z+=v.z;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space on both sides of the += operator on lines 289, 290, and 291.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think res.x, res.y, and res.z need to be initialized to zeroes before the for loop? This might explain the test failures seen on some PostgreSQL versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if num_elements is zero, i.e. if the ARRAY passed to the function is empty? I think it should give an error or return NULL perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the gravity center point (centroid) should be on the sphere in 2D space because pgsphere is about 2D space on the sphere. I'm not sure that the average point in 3d space will be on the sphere. May be to to average long/lat coordinates.

src/point.h Outdated
/*
* Return gravity center from array of spherical points
*/
Datum get_gravity_center(PG_FUNCTION_ARGS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whitespace after Datum probably should be the same as on lines 91 and 86.

pgs_point.sql.in Outdated
@@ -167,3 +167,8 @@ CREATE OPERATOR <-> (
COMMENT ON OPERATOR <-> (spoint, spoint) IS
'distance between spherical points';

CREATE FUNCTION get_gravity_center(dots_vector SPoint[])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial reaction is that the function should be called gravity_center instead of get_gravity_center. I could be wrong about that though. Is there a convention or precedent for how functions should be named?

Copy link
Contributor

@vitcpp vitcpp Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to name it 'centroid'. For example, BOOST.Geometry names such function as centroid.

@esabol
Copy link
Contributor

esabol commented Jul 13, 2023

This constitutes an API change, so the version number will need to be incremented in some fashion. I'm not sure if that's been decided in issue #9 yet. An upgrade script will also be needed.

make test failed. You'll need to update expected/init_test.out.in at least.

make installcheck also failed with most of the tested PostgreSQL versions but not version 10. That's weird. Not sure why that is.

diff -U3 /home/travis/build/postgrespro/pgsphere/expected/points.out /home/travis/build/postgrespro/pgsphere/results/points.out
--- /home/travis/build/postgrespro/pgsphere/expected/points.out	2023-07-13 05:23:20.880316866 +0000
+++ /home/travis/build/postgrespro/pgsphere/results/points.out	2023-07-13 05:24:27.069036698 +0000
@@ -393,7 +393,7 @@
 ]);
     get_gravity_center    
 --------------------------
- (3.0436698 , 0.85893807)
+ (3.0436698 , -1.5707963)
 (1 row)

@esabol
Copy link
Contributor

esabol commented Jul 13, 2023

I'd like to see additional tests added: a test with only one spoint in the passed ARRAY and a test with zero spoints in the passed ARRAY.

@vitcpp
Copy link
Contributor

vitcpp commented Jul 13, 2023

I would propose to name it as 'centroid'. It is more laconic name. Boost geometry names such function as centroid.

Furthermore, we may overload such function for other geometry types to make it complete.

I would also propose to put test SQL for this functionality into a new file like centroid.sql, for example. Such a file separation is reasonable, because all the stuff related to this function will be placed in the single file. If we add some other overloaded functions like centroid(spoly) we will still have the single test file. The test code will not be placed into multiple files.

@vitcpp
Copy link
Contributor

vitcpp commented Jul 13, 2023

I also would consider the case when two points are located in the same big circle in opposite directions. The 3D center mass in this case reside in the center of the sphere. There are two points may be returned. Which point to choose?

Stepan added 3 commits July 19, 2023 11:08
* some little fixes

* some little fixes

* fix tests

* tests

* tests

* reset

* ..

* ..

* fix tests

* fix tests

* curr

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* logs

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* tryings

* try to make unit test

* current state

* tryings

* tryings

* current state

* current state

* current state

* current state

* tryings
@@ -86,7 +86,7 @@
the <literal>y</literal>-axis.
</simpara>
<programlisting>
<![CDATA[sql> SELECT strans ( 20.0*pi()/180.0, -270.0*pi()/180.0, 70.5*pi()/180.0, 'XZY');]]>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pi() call was removed from the formula. I guess it is a typo in the doc.

@vitcpp
Copy link
Contributor

vitcpp commented Jul 19, 2023

Closed until it will be polished in the author's original branch.

@vitcpp vitcpp closed this Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants