【Geometry】Points, Vectors and Normals

Keywords: vector, point, matrix, normal, transformation, Cartesian coordinate system, Cartesian coordinates, spherical coordinates, coordinate system.

关键词:矢量,点,矩阵,法线,变换,笛卡尔坐标系,笛卡尔坐标,球坐标,坐标系。

“Geometry, is a branch of mathematics concerned with questions of shape, size, relative position of figures, and the properties of space.”

“几何学是数学的一个分支,涉及形状,大小,数字的相对位置和空间的性质等问题。

A Word of Warning 警告

This lesson will be long and tedious for most readers. If you are new to the field of computer graphics, take the time to read it carefully though. Fully understanding this part of the CG pipeline is of crucial importance and will save you a lot of time later.

这节课对大多数读者来说是冗长且乏味的。如果您是计算机图形领域的新手,请花点时间仔细阅读。充分理解CG管线的这一部分是非常重要的,以后会节省很多时间。

Introduction to Geometry 几何学入门

Points, vectors, matrices and normals are to computer graphics what the alphabet is to literature; hence most CG books start with a chapter on linear algebra and geometry. However, for many looking to learn graphics programming, presenting a lot of maths before learning about making images can be quite upsetting. If you don’t think CG programming is for you because you do not feel comfortable with maths or don’t understand what a matrix is, don’t give up now.

点,向量,矩阵和法线跟计算机图形学的关系,就像字母跟文学的关系一样。因此大多数书籍都以一个关于线性代数和几何的章节开始。然而,对于许多学习图形编程的人来说,在学习制作图像之前先被塞满大量的数学知识可能会让人感到不安。如你因为对数学感觉不舒服,或者不明白矩阵是什么,而认为CG编程不适合你,现在不要放弃。

We began the “Foundation of 3D Rendering” section with a couple of lessons that do not require any prior knowledge of linear algebra for a reason. While this is a fairly unconventional way of teaching CG programming techniques, we believe it’s more exciting for you to get started with something practical and fun: for example, an introductory ray tracer that requires very minor knowledge of maths and some knowledge of programming. Writing a renderer is a much more exciting and rewarding way of learning maths, as you can see incrementally how certain things are used to produce a concrete result (i.e. your final image). That being said, points, vectors and matrices are instrumental in the process of making CG images; we will use them extensively in pretty much every lesson.

我们的“3D渲染的基础”章节,其中有一些课程不需要线性代数的任何先验知识。虽然这是一种教授CG编程技术的非常规方法,但我们相信,让您以实现一些实用而有趣的事情为开始会更激动人心:例如,介绍性的光线跟踪器需要非常少的数学知识和一些编程知识。写一个渲染器是一个更加令人兴奋和有益的学习数学的方法,正如你可以逐渐看到某些事物被用来产生一个具体的结果(即你最终的图像)。正如我们所说的那样,点、向量、矩阵在制作CG图像的过程中是有帮助的,我们将在每个课程中广泛地使用它们。

In this lesson, you will learn what these constructs are, how they work and the various techniques that can be used to manipulate them. This lesson will also explain the different conventions in linear algebra that CG researchers have used over the years when solving their problems and writing their code. You need to be aware of these conventions as they are very often not mentioned in books (and poorly documented on the web). These conventions are important; before you can read or use another developer’s code or techniques, you must first check what conventions they are using.

在本课中,您将了解这些构造是什么,它们是如何工作的以及可以用来操纵它们的各种技术。本课也将解释CG研究人员在解决问题和编写代码时使用的线性代数中的不同约定。您需要了解这些约定,因为它们在书籍中经常不被提及(并且在网络上记录的很差)。这些约定是重要的,在阅读或使用其他开发人员的代码或技术之前,必须先检查他们正在使用的约定。

One quick note before we begin. If you are a mathematical purist, you might find it strange to see things explained here that are not technically related to linear algebra. We would like to keep the scope of this lesson broad and include simple mathematical techniques commonly used in CG which may only loosely relate to vectors and matrices. For instance, a point, mathematically speaking, has nothing to do with linear algebra (a branch of mathematics only concerned with vectors). We chose to cover points because they are extremely common in CG (and that the same mathematical techniques from linear algebra can be used to manipulate them). If you do not yet understand the distinction between points and vectors, do not worry. We will cover that extensively in this chapter.

在我们开始之前,先做一个简短的说明。如果你是数学纯粹主义者,你可能会觉得很奇怪,因为在这里解释的内容,在技术上与线性代数没有联系。我们希望这一课内容保持广泛,并且包括通常用于CG中简单的数学技术,这些技术可能只与矢量和矩阵松散地相关。例如,一个,从数学上讲,与线性代数(数学的一个分支,只与矢量有关)没有任何关系。我们选择涵盖,因为它们在CG中非常常见(并且可以使用与线性代数相同的数学技巧来操纵它们)。如果你还不明白矢量之间的区别,不要担心。我们将在本章中对此进行广泛的介绍。

What is Linear Algebra? Introduction to Vectors 什么是线性代数?向量入门

So what exactly is linear algebra and what will we study in this lesson? As we mentioned in the previous section, linear algebra is a branch of mathematics that has to do with the study of vectors. Now you might ask, “What is a vector and how is it useful in the CG world?” We won’t get into too much detail, but a vector can be represented as an array of numbers. This array of numbers, which can assume any desired length, is also sometimes called a tuple in mathematics. If we want to be specific about the length of the vector, we may choose to say n-tuple where n represents the number of elements vector. Below is an example of the mathematical notation for a vector with 6 elements:

所以,究竟线性代数是什么?我们将在这节课中学到什么?正如我们在前面提到的那样,线性代数是数学的一个分支,与矢量的研究有关。现在你可能会问,“什么是矢量,它在CG世界中有什么用处?”我们不会涉及太多的细节,但是矢量可以表示为一个数字数组。这个数组可以假定任意长度,在数学中有时也被称为元组。如果我们想具体说明矢量的长度,我们可以选择说n元组,其中n表示矢量中元素的数量。下面是一个有6个元素的矢量的数学符号的例子:

V=(a,b,c,d,e,f), V = ( a , b , c , d , e , f ) ,

where a, b, c, d, e, f are real numbers.

其中a,b,c,d,e,f是实数。

The idea behind grouping these numbers together is that collectively they represent another value or concept that is meaningful in the context of the problem. For example, in computer graphics, vectors can be used to represent either a position or direction in space. We will also be able to transform (or modify) these vectors through a series of operations in a very powerful and compact way. The process of transforming the content of a vector is achieved through what is called a linear transformation. We will spend much more time talking about transformations in a later section; for now, it is only important to recognise that they are very useful.

把这些数字集合在一起的意义是,它们将在一些问题的背景下共同表达另外一个有意义的数值或概念。例如,在计算机图形学中,可以使用矢量来表示空间中的位置或方向。我们也将能够通过一系列操作以非常强大和紧凑的方式来变换(或修改)这些向量。变换矢量的内容这一过程是通过线性变换完成的。我们将在后面的章节中花更多的时间讨论变换。现在,认识到它们非常有用才是重要的。

Points and Vectors 点和向量

The terms point and vector are used throughout many scientific fields in a number of different contexts. In this section, we explain both terms in relation to this tutorial and computer graphics.

术语矢量在许多不同的环境中被用于许多科学领域。在本节中,我们将解释与本教程和计算机图形学有关的这两个术语。

Here, a point is a position in a three-dimensional space. A vector, on the other hand, usually means a direction (and some corresponding magnitude, or size) in three-dimensional space. Vectors can be thought of as arrows pointing various directions. Three-dimensional points and vectors are of course similar in that they are both represented by the aforementioned tuple notation.

这里,一个是三维空间中的一个位置。另一方面,矢量通常意味着在三维空间中的方向(以及一些对应的量值或大小)。向量可以被认为是指向任意方向的箭头。三维点和矢量当然是相似的,因为它们都由前述的元组符号表示。

V=(x,y,z) V = ( x , y , z )

where x, y, z are real numbers.

其中x,y,z是实数。

Figure 1: a point describes a position in space. A vector can be seen as a direction.

Remember, when talking to a mathematician or a physicist, their understanding of a vector or point could be far more general; they are not necessarily restricted to the use we make of them in CG. For them, a vector could be of arbitrary or even infinite size (meaning it can contain as many numbers as desired).

记住,当与数学家或物理学家交谈时,他们对矢量或点的理解可能更为一般;它们不一定限于我们在cg中使用它们。对于他们来说,一个矢量可以是任意的,甚至是无限的大小(意味着它可以包含尽可能多的数字)。

We will finish this chapter by briefly mentioning homogeneous points. Sometimes it is necessary to add a fourth element for mathematical convenience. An example of a point with homogeneous coordinates is given below:

我们将通过简要提及其次点来完成本章。有时为了数学的方便,需要添加第四个元素。下面给出一个具有齐次坐标的点的例子:

PH=(x,y,z,w) P H = ( x , y , z , w )

Homogeneous points are used when it comes to multiplying points with matrices. Don’t worry too much about them at this point of the lesson. We just mention them now as they sometimes appear in the literature and can be confusing to readers. They will be explained in detail later in this lesson.

当用矩阵乘点时,会使用齐次点。在这节课中,不要太担心他们。我们现在只是提及他们,因为他们有时出现在文章中,可能会让读者感到困惑。他们将在本课的后面详细解释。

A Quick Introduction to Transformations 变换快速入门

You might still be wondering what effect a linear transformation has on points and vectors. It’s actually quite simple. One of the most common operations we perform on points in CG consists of simply moving them around in space. This transformation is more specifically called translation and it plays a vital role in the rendering process.

你可能仍然想知道线性变换对点和矢量有什么影响。其实很简单,我们在cg中执行的最常见操作之一就是简单地在空间中移动它们。这种变换更具体地称为平移,它在渲染过程中起着至关重要的作用。

The translation operator is nothing more than a linear transformation of the original point (which can be viewed as an input position point). Applied to a vector (which, remember, is a direction), translation has no meaning. This is because where the vector begins (that is, where it is centered) is not important; regardless of position, all “arrows” of the same length, pointing in the same direction, are equivalent. Instead, we very commonly use another linear transformation on vectors: rotation. Many more common operators can be used, but for now, let’s just consider translation for points and rotations for vectors.。

平移运算符只不过是原始点的线性变换(可以将其视为输入位置点)。应用于一个矢量时(记住,这是一个方向),平移没有意义。这是因为矢量开始的地方(也就是它的中心)并不重要。不管位置如何,所有指向相同方向的长度相同的“箭头”是相同的。相反,我们通常在向量上使用另一个线性变换:旋转。我们可以使用更多常见的操作符,但现在,我们只考虑向量的旋转和点的平移。

PTranslatePTVRotateVT P → T r a n s l a t e → P T V → R o t a t e → V T

The subscripted letter T stands for “transformed”.

字母下标 T 表示转换过的。

As you may have noticed, up to now, we have failed to discuss what the length, or magnitude, of a vector means. Indeed, the length of the arrow has a great deal of importance in CG. When the length of a vector is exactly 1, we say that the vector is normalised (you will hear and read this term all the time). The act of normalising a vector involves altering the vector such that its length becomes 1, but its direction remains unchanged. Most of the time, we will want our vectors to be normalised. However, in some cases, not normalising them can be preferred as the length of the vector will be meaningful.

正如你可能已经注意到的,到现在为止,我们没有讨论矢量的长度或者幅度意味着什么。
事实上,一个箭头的长度在cg中是非常重要的。当矢量的长度正好是1时,我们说矢量是归一化的(你会一直听到并读出这个词)。矢量归一化的行为涉及改变矢量,使其长度变为1,但其方向保持不变。大多数时候,我们会希望我们的向量被归一化。然而,在某些情况下,由于矢量的长度将是有意义的,因此不优化它们可以是优选的。

For instance, imagine that you trace a line from point A to point B. The line created is a vector in the sense that it indicates where point B is located relative to point A. That is, it gives the direction of B as if you were standing at point AA. The length of the vector in this case indicates the distance from A to B. This distance is sometimes required in certain algorithms.

例如,想象你从A点到B点的一条线。所创建的线是一个矢量,它表示点B相对于点A的位置。也就是说,它给出了B的方向,就像你站在A点一样。在这种情况下矢量的长度表示从AB的距离。在某些算法中有时需要这个距离。

Normalisation of vectors is often a source of bugs in applications and every time you declare a vector (or even use one), we recommend that you always consciously ask yourself if this vector is/isn’t or should/shouldn’t be normalised.

向量的归一化往往是应用程序中的bug的来源,每当你声明一个向量(甚至使用一个向量),我们建议你总是有意识地问自己,这个向量是否应该/不应该被归一化。

Normals 法线

image

A normal is the technical term used in Computer Graphics (and Geometry) to describe the orientation of a surface of a geometric object at a point on that surface. Technically, the surface normal to a surface at point P, can be seen as the vector perpendicular to a plane tangent to the surface at P. Normals play an important role in shading where they are used to compute the brightness of objects (see further lessons on Lights and Shading).

法线是在计算机图形(和几何)中用来描述在该表面上的某个点处的几何对象的表面的方向的技术术语。从技术上讲,P点的表面法线,可以看作由P点出发的,垂直于与P点相切的平面的向量。法线在用于计算物体亮度的阴影中起着重要的作用(在灯光和阴影章节将介绍更多)。

Normals can be thought of as vectors with one caveat: they do not transform the same way that vectors do. This is one of main reasons we take the time to differentiate them. You will find more information on this topic in the chapter Transforming Normals. For now, it is only important to understand what they are.

法线可以被认为是一个向量,但要注意:它们不像向量那样进行变换。这是我们花时间区分它们的主要原因之一。在转换法线的章节中可以找到关于这个主题的更多信息。现在只知道它们是什么。

From Theory to C++ 从理论到C++

In our C++ code, we won’t make the distinction between points, vectors, and normals; we represent all three with a Vec3 class (a class template so that we can create float, int or double versions as needed). Some developers prefer to differentiate them. This clearly limits the possibility of making mistakes. From experience, we found it more efficient (less code to write in the first place) to just deal with one unique class (as the OpenEXR library does). However, we will still have to call a few specific functions carefully depending on whether or not the Vec3 we are dealing with represents a point, a vector or a normal. As you may remember, this is particularly critical when we use transformations. The full source code is provided in the download section of this lesson.

在我们的c++代码中,我们不会区分点,矢量和法线;我们用vec3类(一个类模板,以便我们可以根据需要创建float,int或double版本)来表示所有这三个类。一些开发人员更喜欢区分它们。这显然限制了犯错误的可能性。从经验来看,我们发现它更高效(首先编写的代码更少)来处理一个独特的类(就像openexr库一样)。然而,我们仍然需要仔细地调用一些特定的函数,取决于我们所处理的vec3是否代表一个点,一个向量或一个法线。你可能还记得,当我们使用转换的时候,这一点尤其重要。完整的源代码在本课的下载部分提供。

template<typename T> 
class Vec3 
{ 
public: 
    // 3 most basic ways of initializing a vector
    Vec3() : x(T(0)), y(T(0)), z(T(0)) {} 
    Vec3(const T &xx) : x(xx), y(xx), z(xx) {} 
    Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {} 
    T x, y, z; 
}; 

typedef Vec3<float> Vec3f; 

Vec3<float> a; 
Vec3f b; 
Summary 总结

From this first chapter, you should remember that mathematically a vector can be of any dimension. However in CG, we use a more specific definition: a vector is a direction in 3D space (and therefore represented by three numbers). Additionally, we talk of points as representations of positions (also in 3D space and also represented by three numbers). Homogeneous points are represented with four numbers but are a particular case that we will study later.

从第一章开始,你应该记住,在数学上矢量可以是任何维度。然而在CG中,我们使用更具体的定义:矢量是3D空间中的方向(因此用三个数字表示)。此外,我们还谈到作为位置的表示(也在三维空间,也用三个数字表示)。其次点用四个数字表示,但是我们稍后将研究一个特例。

Points and vectors can be transformed using linear transformations.

点和矢量可以使用线性变换进行变换。

You will see the term linear transformation being used often. If lines are preserved while being transformed, then we speak of linear transformation (multiplication by a matrix is a linear transformation).

你会看到经常使用的术语“线性变换”。如果线条在被转换时被保留下来,那么我们讲线性转换(乘以一个矩阵就是一个线性转换)。

Typical examples of such transformations are translation for points and rotation for vectors. The length of a vector can be set to 1, in which case we say that it is normalised. The length of a vector (before it is normalised) represents the distance between two points and is sometimes needed in certain algorithms. For this reason, a developer has to be careful as to when and why he/she potentially chooses to normalise a vector.

这种变换的典型例子是点的平移和向量的旋转。向量的长度可以被设为1,这种情况下我们说向量是归一化的。向量的长度(被归一化之前)表示了两点之间的距离,在某些算法中需要被使用。因此,开发者在归一化向量时必须要谨慎。

What’s Next? 下一步?

One important thing we haven’t explained yet is what the three numbers defining points and vectors represent. These number represent the coordinates of a point (in 2D or 3D space) with respect to a reference (also sometimes called the origin). This reference, which we technically call a coordinate system, is the topic of our next chapter.

我们还没有解释的一个重要的事情是三个定义点和矢量的数字代表什么。
这些数字表示相对于参考(有时也称为原点)的点(在2d或3d空间中)的坐标。
这个我们在技术上称之为坐标系,是我们下一章的主题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值