Skip to content

Commit 511dfc1

Browse files
committed
Remove some static_cast
1 parent 0bcf215 commit 511dfc1

File tree

8 files changed

+33
-36
lines changed

8 files changed

+33
-36
lines changed

Frame/Source/Assignment/Assignment2/rasterizer.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t) {
134134
{
135135
for (uint16_t pos_y = min_y; pos_y <= max_y; ++pos_y)
136136
{
137-
size_t index = static_cast<size_t>(get_index(static_cast<int>(pos_x), static_cast<int>(pos_y)));
138-
float &depth = depth_buf[index];
137+
float &depth = depth_buf[get_index((int)pos_x, (int)pos_y)];
139138

140139
// SSAA 所采样的四个子像素相对于像素左下角坐标的偏移量
141140
static const std::vector<Eigen::Vector2f> s_offsets = { {0.25f, 0.25f}, {0.75f, 0.25f}, {0.25f, 0.75f}, {0.75f, 0.75f} };
@@ -150,8 +149,8 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t) {
150149

151150
for (const auto& offset : s_offsets)
152151
{
153-
float subPos_x = static_cast<float>(pos_x) + offset.x();
154-
float subPos_y = static_cast<float>(pos_y) + offset.y();
152+
float subPos_x = (float)pos_x + offset.x();
153+
float subPos_y = (float)pos_y + offset.y();
155154

156155
// 获取子像素的深度插值
157156
auto [alpha, beta, gamma] = computeBarycentric2D(subPos_x, subPos_y, t.v);
@@ -174,14 +173,14 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t) {
174173
}
175174
}
176175

177-
finalColor /= static_cast<float>(activeColor);
178-
finalDepth /= static_cast<float>(activeDepth);
176+
finalColor /= (float)activeColor;
177+
finalDepth /= (float)activeDepth;
179178

180179
// 修复框架 bug
181180
if (finalDepth > depth)
182181
{
183182
depth = finalDepth;
184-
set_pixel(Eigen::Vector3f{ static_cast<float>(pos_x), static_cast<float>(pos_y), finalDepth }, finalColor);
183+
set_pixel(Eigen::Vector3f{ (float)pos_x, (float)pos_y, finalDepth }, finalColor);
185184
}
186185
}
187186
}

Frame/Source/Assignment/Assignment3/Texture.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class Texture
4444
u = std::clamp(u, 0.0f, 1.0f);
4545
v = std::clamp(v, 0.0f, 1.0f);
4646

47-
int u_index = static_cast<int>(u * static_cast<float>(width));
48-
int v_index = static_cast<int>((1.0f - v) * static_cast<float>(height));
47+
int u_index = u * width;
48+
int v_index = (1.0f - v) * height;
4949
u_index = std::clamp(u_index, 0, width - 1);
5050
v_index = std::clamp(v_index, 0, height - 1);
5151

@@ -57,8 +57,8 @@ class Texture
5757
u = std::clamp(u, 0.0f, 1.0f);
5858
v = std::clamp(v, 0.0f, 1.0f);
5959

60-
float widthf = static_cast<float>(width);
61-
float heightf = static_cast<float>(height);
60+
float widthf = (float)width;
61+
float heightf = (float)height;
6262

6363
float u_img = u * widthf;
6464
float v_img = (1.0f - v) * heightf;
@@ -71,10 +71,10 @@ class Texture
7171
Eigen::Vector2f u11{ std::min(widthf, center.x() + 0.5f), std::min(heightf, center.y() + 0.5f) };
7272

7373
// 边界情况,假设次采样点坐标为 (width, height),它应当采样的下标则为 (width - 1, height - 1)
74-
Eigen::Vector2i u00_index{ std::clamp(static_cast<int>(u00.x()), 0, width - 1), std::clamp(static_cast<int>(u00.y()), 0, height - 1) };
75-
Eigen::Vector2i u01_index{ std::clamp(static_cast<int>(u01.x()), 0, width - 1), std::clamp(static_cast<int>(u01.y()), 0, height - 1) };
76-
Eigen::Vector2i u10_index{ std::clamp(static_cast<int>(u10.x()), 0, width - 1), std::clamp(static_cast<int>(u10.y()), 0, height - 1) };
77-
Eigen::Vector2i u11_index{ std::clamp(static_cast<int>(u11.x()), 0, width - 1), std::clamp(static_cast<int>(u11.y()), 0, height - 1) };
74+
Eigen::Vector2i u00_index{ std::clamp((int)u00.x(), 0, width - 1), std::clamp((int)u00.y(), 0, height - 1) };
75+
Eigen::Vector2i u01_index{ std::clamp((int)u01.x(), 0, width - 1), std::clamp((int)u01.y(), 0, height - 1) };
76+
Eigen::Vector2i u10_index{ std::clamp((int)u10.x(), 0, width - 1), std::clamp((int)u10.y(), 0, height - 1) };
77+
Eigen::Vector2i u11_index{ std::clamp((int)u11.x(), 0, width - 1), std::clamp((int)u11.y(), 0, height - 1) };
7878

7979
Eigen::Vector3f u00_value = Vec3bToVector3f(image_data.at<cv::Vec3b>(u00_index.y(), u00_index.x()));
8080
Eigen::Vector3f u01_value = Vec3bToVector3f(image_data.at<cv::Vec3b>(u01_index.y(), u01_index.x()));

Frame/Source/Assignment/Assignment3/rasterizer.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,18 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t, const std::array<Eig
139139
{
140140
for (uint16_t pos_y = min_y; pos_y <= max_y; ++pos_y)
141141
{
142-
if (!insideTriangle(static_cast<float>(pos_x) + 0.5f, static_cast<float>(pos_y) + 0.5f, t.v))
142+
if (!insideTriangle((float)pos_x + 0.5f, static_cast<float>(pos_y) + 0.5f, t.v))
143143
{
144144
continue;
145145
}
146146

147-
auto [alpha, beta, gamma] = computeBarycentric2D(static_cast<float>(pos_x), static_cast<float>(pos_y), t.v);
147+
auto [alpha, beta, gamma] = computeBarycentric2D((float)pos_x, static_cast<float>(pos_y), t.v);
148148
float alpha_w = alpha / v[0].w();
149149
float beta_w = beta / v[1].w();
150150
float gamma_w = gamma / v[2].w();
151151
float final_z = 1.0f / (alpha_w + beta_w + gamma_w);
152152

153-
size_t index = static_cast<size_t>(get_index(static_cast<int>(pos_x), static_cast<int>(pos_y)));
153+
size_t index = static_cast<size_t>(get_index((int)pos_x, (int)pos_y));
154154
float &depth = depth_buf[index];
155155

156156
// 修复框架 bug
@@ -170,9 +170,7 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t, const std::array<Eig
170170
std::move(texCoords),
171171
texture.has_value() ? &(texture.value()) : nullptr);
172172

173-
set_pixel(
174-
{ static_cast<float>(pos_x),static_cast<float>(pos_y) },
175-
fragment_shader(std::move(payload)));
173+
set_pixel({ (int)pos_x, (int)pos_y }, fragment_shader(std::move(payload)));
176174
}
177175
}
178176
}

Frame/Source/Assignment/Assignment5/Renderer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void Renderer::Render(const Scene& scene)
213213
std::vector<Vector3f> framebuffer(scene.width * scene.height);
214214

215215
float scale = std::tan(deg2rad(scene.fov * 0.5f));
216-
float imageAspectRatio = static_cast<float>(scene.width) / static_cast<float>(scene.height);
216+
float imageAspectRatio = (float)scene.width / (float)scene.height;
217217

218218
// Use this variable as the eye position to start your rays.
219219
Vector3f eye_pos(0);
@@ -224,13 +224,13 @@ void Renderer::Render(const Scene& scene)
224224
{
225225
// https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-generating-camera-rays/generating-camera-rays.html
226226

227-
float x = (2.0f * ((i + 0.5f) / static_cast<float>(scene.width)) - 1.0f) * scale * imageAspectRatio;
228-
float y = (1.0f - 2.0f * ((j + 0.5) / static_cast<float>(scene.height))) * scale;
227+
float x = (2.0f * ((i + 0.5f) / (float)scene.width) - 1.0f) * scale * imageAspectRatio;
228+
float y = (1.0f - 2.0f * ((j + 0.5) / (float)scene.height)) * scale;
229229

230230
Vector3f dir = normalize(Vector3f(x, y, -1));
231231
framebuffer[index++] = castRay(eye_pos, dir, scene, 0);
232232
}
233-
UpdateProgress(j / static_cast<float>(scene.height));
233+
UpdateProgress(j / (float)scene.height);
234234
}
235235

236236
// save framebuffer to file

Frame/Source/Assignment/Assignment6/BVH.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ BVHBuildNode* BVHAccel::recursiveBuild(std::vector<Object*> objects)
102102

103103
// 划分方式的总数
104104
constexpr uint8_t SlashCount = 8;
105-
constexpr float SlashCountInv = 1.0f / static_cast<float>(SlashCount);
105+
constexpr float SlashCountInv = 1.0f / (float)SlashCount;
106106
const float SC = centroidBounds.SurfaceArea();
107107

108108
// 用于记录最优的划分方式
@@ -172,9 +172,9 @@ Intersection BVHAccel::Intersect(const Ray& ray) const
172172
Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
173173
{
174174
std::array<int, 3> dirIsNeg{
175-
static_cast<int>(ray.direction.x < 0.0f),
176-
static_cast<int>(ray.direction.y < 0.0f),
177-
static_cast<int>(ray.direction.z < 0.0f)
175+
(int)(ray.direction.x < 0.0f),
176+
(int)(ray.direction.y < 0.0f),
177+
(int)(ray.direction.z < 0.0f)
178178
};
179179
if (!node->bounds.IntersectP(ray, ray.direction_inv, std::move(dirIsNeg)))
180180
{

Frame/Source/Assignment/Assignment7/Renderer.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void Renderer::Render(const Scene &scene, const uint32_t spp)
1818
std::cout << "SPP: " << spp << std::endl;
1919

2020
float scale = tan(deg2rad(scene.fov * 0.5));
21-
float imageAspectRatio = static_cast<float>(scene.width) / static_cast<float>(scene.height);
21+
float imageAspectRatio = (float)scene.width / (float)scene.height;
2222
Vector3f eyePosition(278, 273, -800);
2323

2424
size_t index = 0;
@@ -28,18 +28,18 @@ void Renderer::Render(const Scene &scene, const uint32_t spp)
2828
{
2929
for (uint32_t widthIndex = 0; widthIndex < scene.width; ++widthIndex)
3030
{
31-
float x = (2 * (widthIndex + 0.5f) / static_cast<float>(scene.width) - 1) * imageAspectRatio * scale;
32-
float y = (1 - 2 * (heightIndex + 0.5f) / static_cast<float>(scene.height)) * scale;
31+
float x = (2.0f * (widthIndex + 0.5f) / (float)scene.width - 1.0f) * imageAspectRatio * scale;
32+
float y = (1.0f - 2.0f * (heightIndex + 0.5f) / (float)scene.height) * scale;
3333
Vector3f rayDir = normalize(Vector3f(-x, y, 1.0f));
3434

3535
#pragma omp parallel for
3636
for (int sppCount = 0; sppCount < spp; ++sppCount)
3737
{
38-
framebuffer[index] += scene.castRay(Ray(eyePosition, rayDir), 0) / static_cast<float>(spp);
38+
framebuffer[index] += scene.castRay(Ray(eyePosition, rayDir), 0) / (float)spp;
3939
}
4040
++index;
4141
}
42-
UpdateProgress(heightIndex / static_cast<float>(scene.height));
42+
UpdateProgress(heightIndex / (float)scene.height);
4343
}
4444
UpdateProgress(1.0f);
4545

Frame/Source/Assignment/Assignment7/global.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ inline void UpdateProgress(const float progress)
3030
else if (i == pos) std::cout << ">";
3131
else std::cout << " ";
3232
}
33-
std::cout << "] " << static_cast<int>(progress * 100.0f) << " %\r";
33+
std::cout << "] " << (int)(progress * 100.0f) << " %\r";
3434
std::cout.flush();
3535
};

Frame/Source/Assignment/Assignment8/rope.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Rope::Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k
1818

1919
for (size_t i = 0; i < num_nodes; ++i)
2020
{
21-
Vector2D pos = start + (end - start) * (static_cast<float>(i) / (static_cast<float>(num_nodes) - 1.0f));
21+
Vector2D pos = start + (end - start) * ((float)i / ((float)num_nodes - 1.0f));
2222
masses.emplace_back(new Mass{ pos, node_mass, false });
2323
}
2424

0 commit comments

Comments
 (0)