What's the equation for calculating normal vector at a point?

119 Views Asked by At

What's the equation for calculating normal vector at a point?

Particularly I'm trying understand the function given here:

Vec3f Sphere::GetNormalAt(const Vec3f &pos) const {   
    return cv::normalize(pos - points[o]); 
}

So what exactly is this as an equation?

It's some kind of

$$\frac{pos-p}{||pos-p||}$$

but what point is $p$?

Also, I'm particularly trying to get this function in order to use here: https://bobobobo.wordpress.com/tag/ray/
where you see the function getNormalAt() being used for something in Ray-Sphere interaction.

Additionally here one can find the function with some algebra:

private Vector GetNormalAt(Vector v)
{
    double x = (2 / (_a * _a)) * v.GetX();
    double y = (2 / (_b * _b)) * v.GetY();
    double z = (2 / (_c * _c)) * v.GetZ();
    Vector normal = new Vector(x, y, z);
    normal.NornVector();
    return normal * -_normalSign;
}
1

There are 1 best solutions below

0
On BEST ANSWER

Well, looking at the function, it is a method of a sphere class.

Consider a sphere and a point $p$ on the sphere, of radius $r$, with center $c$.

The normal at $p$ is pointing in the same direction as $n = p - c$ (i.e. pointing outward from the center of the sphere). Thus, the unit normal is just: $$ n(p) = \frac{p-c}{||p-c||_2} $$ So I can only assume $c$ is the point to which you are referring.

The second class you link to is an ellipsoid class. Again, the calculation is specific to that surface.


For reference, in general, one computes surface normals by finding two linearly independent tangent vectors to the surface $T_1(p)$ and $T_2(p)$ at point $p$, and computing the normal as: $$ n(p) = \frac{T_1(p)\times T_2(p)}{||T_1(p)\times T_2(p)||_2} $$

(The cases you are looking at have special geometry such that this is not required.)