Happy Thanksgiving everyone,
I have a collection of various simple geometric objects (point, ray,
cube, box, sphere, pyramid, etc) and I want to write (fast, and thus
specific) functions that returns whether two objects intersect. I am
unsure how to structure this setup.
I see it in two basic ways. First, in each class I could add a member
function:
class Ray {
...
bool Intersects( const Box& box ) { ... }
bool Intersects( const Sphere& sphere ) { ... }
bool Intersects( ... ) { ... } /* Rest of objects */
...
};
Or I could place all "bool Intersects( ..., ... )" functions in a
single file for all pairs:
bool Intersects( const Ray& ray, const Box& box ) { ... }
....
bool Intersects( const Box& box, const Sphere& sphere ) { ... }
....
My number of geometric objects is limited so that writing all (N choose
2) functions is managable.
The first way seems more advantageous if each geometric object had a
common subclass (is this true?), but it seems messier because every
geometric class has to be able to "see" every other geometric class.
For example, adding a cone class would require me to edit every single
file. The second way seems more abstract in that each object need not
know of the others and all intersection code is centralized into a
single area.
Is there a better, solid, or more convectional way of doing this?
Thanks in advance,
Brian Byrne 4 1556
Brian Byrne napsal:
Happy Thanksgiving everyone,
I have a collection of various simple geometric objects (point, ray,
cube, box, sphere, pyramid, etc) and I want to write (fast, and thus
specific) functions that returns whether two objects intersect. I am
unsure how to structure this setup.
I see it in two basic ways. First, in each class I could add a member
function:
class Ray {
...
bool Intersects( const Box& box ) { ... }
bool Intersects( const Sphere& sphere ) { ... }
bool Intersects( ... ) { ... } /* Rest of objects */
...
};
Or I could place all "bool Intersects( ..., ... )" functions in a
single file for all pairs:
I would use the second variant (separate functions).
One additional hint (which you probably know): You do not need all the
pairs, because intersection of A and B is the same as intersection of B
and A, so you can call only the other functions with swapped parameters.
On 2006-11-23 20:58, Brian Byrne wrote:
Happy Thanksgiving everyone,
I have a collection of various simple geometric objects (point, ray,
cube, box, sphere, pyramid, etc) and I want to write (fast, and thus
specific) functions that returns whether two objects intersect. I am
unsure how to structure this setup.
I see it in two basic ways. First, in each class I could add a member
function:
class Ray {
...
bool Intersects( const Box& box ) { ... }
bool Intersects( const Sphere& sphere ) { ... }
bool Intersects( ... ) { ... } /* Rest of objects */
...
};
Or I could place all "bool Intersects( ..., ... )" functions in a
single file for all pairs:
bool Intersects( const Ray& ray, const Box& box ) { ... }
...
bool Intersects( const Box& box, const Sphere& sphere ) { ... }
...
My number of geometric objects is limited so that writing all (N choose
2) functions is managable.
That depends on how large N you have, a quick calculation by me (which
means that it's not guaranteed to be correct) gives (N(N+1))/2 functions
for N objects (that's O(N^2)).
The first way seems more advantageous if each geometric object had a
common subclass (is this true?), but it seems messier because every
geometric class has to be able to "see" every other geometric class.
You mean common base-class right?
For example, adding a cone class would require me to edit every single
file. The second way seems more abstract in that each object need not
know of the others and all intersection code is centralized into a
single area.
Is there a better, solid, or more convectional way of doing this?
Can't really help you but if I were you I'd spend a couple of hours
thinking about finding a generic way to find out if two shapes
intersect, perhaps by dividing them into smaller pieces and see if the
pieces intersect. If you can do this then you can let them all derive
from a common base-class Shape. Then define a method
bool intersect(Shape& s)
which you override in each subclass. If you can manage this then you
only have to implement one method, that checks if a Shape intersect with
it, for each shape. This should be possible for simpler shapes.
--
Erik Wikström
Brian Byrne wrote:
Happy Thanksgiving everyone,
I have a collection of various simple geometric objects (point,
ray, cube, box, sphere, pyramid, etc) and I want to write (fast,
and thus specific) functions that returns whether two objects
intersect. I am unsure how to structure this setup.
I see it in two basic ways. First, in each class I could add a
member function:
class Ray {
...
bool Intersects( const Box& box ) { ... }
bool Intersects( const Sphere& sphere ) { ... }
bool Intersects( ... ) { ... } /* Rest of objects */
...
};
Or I could place all "bool Intersects( ..., ... )" functions in a
single file for all pairs:
bool Intersects( const Ray& ray, const Box& box ) { ... }
...
bool Intersects( const Box& box, const Sphere& sphere ) { ... }
...
My number of geometric objects is limited so that writing all (N
choose 2) functions is managable.
If I'm not mistaken, the number of needed functions is N^2 for N
object types, of which you really need to implement only N(N+1)/2 as
Ondra and Erik pointed it out (which leaves N(N-1)/2 inline
forwarders).
The first way seems more advantageous if each geometric object had
a common subclass,
All the objects you mentionned (but the ray) should really derive
from a common class "BoundingSphere". This is the cheapest way I
know of, to be able to tell if two objects _may_ intersect, before
doing the real (and expensive, yet specific) computations.
You just need to ensure a BoundingSphere's center and radius are
adjusted by it's derived class everytime it is moved/resized, to
maintain the invariant.
(is this true?) but it seems messier because
every geometric class has to be able to "see" every other
geometric class. For example, adding a cone class would require me
to edit every single file. The second way seems more abstract in
that each object need not know of the others and all intersection
code is centralized into a single area.
Is there a better, solid, or more convectional way of doing this?
I'd personnaly go for the second way you mentionned, which seems
cleaner.
Additionnaly, I'd use a common ancestor for all objects (including
rays), which would allow for a "generic" dispatcher function in case
you only have references or pointers to base objects.
Something like...
struct SpatialObject
{
// Override in each subclass. The subclass knows it's own type,
// so it only has to dynamically determine other's type before
// calling the appropriate Intersects overload.
virtual bool Intersects(const SpatialObject& other) const = 0;
};
bool Intersects(const Ray&, const Box&);
//...
Note that this design is only useful if you need runtime
polymorphism, which is very likely IMO even though you didn't
mention it.
Hope this helps a bit.
--
IR
On 23 Nov 2006 11:58:27 -0800 in comp.lang.c++, "Brian Byrne"
<bd*****@gmail.comwrote,
>Is there a better, solid, or more convectional way of doing this?
This seems obviously to be the kind of problem where a few weeks of
carefree trial and error hacking can save hours of boring research... or
at least postpone it. For the N x N aspect of things, look for "double
dispatch" or "visitor pattern". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lester |
last post by:
a pre-beginner's question:
what is the pros and cons of .net, compared to ++
I am wondering what can I get if I continue to learn C# after I have learned
C --> C++ --> C# ??
I think there...
|
by: z_learning_tester |
last post by:
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?
The example is usually something like:
Turn...
|
by: C_beginner |
last post by:
Am I did the following program corectly according to the question?
Question:
/* 1:You have just been employed by MacroMuscle, Inc.
(Software for Hard Bodies). The company is entering the...
|
by: --- |
last post by:
I'm writing up a sample program to teach someone the basics of
database application design. I'm using the 2.0 framework and
Microsofts default "pubs" database.
What I'd like to do is create a...
|
by: anke |
last post by:
hi all,
i am fairly new to css. i know the very basics, can work with scripts and adapt them. i learned webdesign in the late 90ies and have troubles forget about my beloved tables ;-)
anyway,...
|
by: Johs |
last post by:
I am looking for a good C++ book for beginners. I have some experience
with C and a lot of experience with Java. I am currently reading Bjarne
Stroustrups C++ Programming Language but it starts off...
|
by: macca |
last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP
beginner - Reccommendations please?
Thanks
Paul
|
by: Solo.Wolve |
last post by:
I've just finished the K&R c, And begin to study c++ myself,so can
anyone give me some advice?such as some books to read,and something to
notice?
Thank you very much.
|
by: subramanian100in |
last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei
Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit
conversions", the authors have advised the use of named...
|
by: nicholas08 |
last post by:
I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item.
Attaching the menu so it's clear...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |