tags:

views:

151

answers:

4

I came accross this: t = Clamp(t/d, 0, 1) but I'm not sure how to perform this operation on a vector. What are the steps to clamp a vector if one was writing their own vector implementation?

Thanks

clamp clamping a vector to a minimum and a maximum

ex:

pc = # the point you are coloring now
p0 = # start point
p1 = # end point
v = p1 - p0
d = Length(v)
v = Normalize(v) # or Scale(v, 1/d)

v0 = pc - p0

t = Dot(v0, v)
t = Clamp(t/d, 0, 1)

color = (start_color * t) + (end_color * (1 - t))
A: 
clamp(vec, lb, ub) == min(max(vec, lb), ub)

edit

min and max are usually primitive operations on vectors. For example, if you're using SSE vectors, there are _mm_min_ps and _mm_max_ps intrinsics that turn into MINPS and MAXPS instructions on x86.

Chris Dodd
What are the min and max functions?
Milo
@user146780: That's what everyone's asking you in the comments to your question!
Jefromi
min and max are defined in the c++ library in <algorithm>
gbrandt
And they assume `bool operator<(T, T)` exists. Typically they don't exist for a vector. Is (-1,0,0) < (0.5,0,0) ?
MSalters
A: 

I think that once you state clearly what you mean you'll find that most of the work is done for you...

I'm guessing you want to limit the length of the vector quantity (rather than a vector data structure) to lie within a specified range without changing its direction, no?

So:

if (v.length > max)
   v.setlength(max)
else if (v.length < min)
   v. setlength(min)

where the implementation of length() and setlength() depend on how you have stored your vector.


If your vector is stored in (angle,magnitude) form this is nearly trivial. If stored in Cartesian form (ie. (x,y,z) ) you get length from the Pythagorian theorem and setlength should scale each commponent by a factor of desired_length/current_length.

dmckee
A: 

Well, I'd assuming you'd want to clamp each of the coordinates individually. So...

void clamp(const Vector3 &v, const Vector3 &min, const Vector3 &max)
{
    v.x = clamp(v.x, min.x, max.x);
    v.y = clamp(v.y, min.y, max.y);
    v.z = clamp(v.z, min.z, max.z);
}

int clamp(int value, int min, int max)
{
    if (value < min)
    {
        return min;
    }
    else if (value > max)
    {
        return max;
    }

    return value;
}

Hope that helps.

Jake Petroules
Clamping coordinates independently may change the direction of the vector, which is not how I interpreted the request.
dmckee
I was thinking of a vector in the context of 3d graphics. A clamp function could be useful there in ensuring a point remains within a cube. I didn't really know what he was asking.
Jake Petroules
Even if you wanted to make sure that the vector remains in a cube (or a sphere) there's still the question whether you want to keep the direction.
MSalters
A: 

The easiest answer is when you consider a vector in a spherical coordinate system: {R, φ, θ}. φ and θ are already limited to [-π,+π] and [-½π, +½π] anyway. Hence, you only need to clamp the distance from the origin R.

MSalters