Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HEADER
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include <string>
- /**
- *
- */
- class LearnUnreal_API PointcloudSmoothener
- {
- public:
- PointcloudSmoothener(TCHAR* path);
- ~PointcloudSmoothener();
- //Make public for testing
- TArray<FVector> Points;
- TArray<FVector> smoothByAvarage(TArray<FVector> points);
- private:
- TArray<FVector> constructPointVectors(TArray<FString> data);
- };
- //Source File
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "LearnUnrealTest.h"
- #include "PointcloudSmoothener.h"
- #include <stdlib.h>
- //Read the file contents and construct the meshPoints
- PointcloudSmoothener::PointcloudSmoothener(TCHAR* path)
- {
- TArray<FString> Lines;
- bool ReadSuccess = FFileHelper::LoadANSITextFileToStrings(path, NULL, Lines);
- if (ReadSuccess) {
- Points = constructPointVectors(Lines);
- Points = smoothByAvarage(Points);
- }
- }
- PointcloudSmoothener::~PointcloudSmoothener()
- {
- }
- //Smooth out the pointcloud removing unnecessary points
- TArray<FVector> smoothByAvarage(TArray<FVector> points)
- {
- TArray<FVector> pointList;
- pointList.Reserve(points.Num() - 2);
- for (int i = 2; i < points.Num(); i++) {
- pointList[i - 2] = points[i];
- }
- size_t distance = 4;
- size_t end = pointList.Num() - distance;
- FVector basePlaneDirection = (pointList[distance] - pointList[0]);
- basePlaneDirection.Normalize();
- FVector newPlaneDirection;
- TArray<FVector> smoothPointCloud;
- smoothPointCloud.Reserve(2);
- smoothPointCloud[0] = points[0];
- smoothPointCloud[1] = points[1];
- smoothPointCloud.Add(pointList[0]);
- for (size_t i = 1; i < end; i++) {
- newPlaneDirection = pointList[(i + distance) % (pointList.Num())] - pointList[i];
- FVector NormalizedBaseVector = basePlaneDirection;
- NormalizedBaseVector.Normalize();
- FVector NormalizedNewvector = newPlaneDirection;
- NormalizedNewvector.Normalize();
- //If a sharp turn/angle
- if (FVector::DotProduct(NormalizedBaseVector, NormalizedNewvector) < 0.7f) {
- smoothPointCloud.Add(pointList[i]);
- //smoothPointCloud.Add(pointList[i]);
- basePlaneDirection = NormalizedNewvector;
- }
- }
- smoothPointCloud.Shrink();
- return smoothPointCloud;
- }
- //Parse the read file to an array of Point vectors.
- TArray<FVector> PointcloudSmoothener::constructPointVectors(TArray<FString> data)
- {
- //fprintf(stderr, "%s", data.c_str());
- TArray<FVector> points;//Size = 2 * numLines + 2.
- TArray<std::string> floats;//Size = 2 * numLines + 2.
- size_t pos = 0;
- size_t prev = 0;
- std::string token;
- //Split into Floats
- std::string delimiter = " ";
- for (int i = 0; i < data.Num(); i++) {
- std::string currLine = std::string(TCHAR_TO_UTF8(*data[i]));
- while ((pos = currLine.find(delimiter, prev)) != std::string::npos) {
- token = currLine.substr(prev, pos - prev);
- //fprintf(stderr, "%s\n", token.c_str());
- floats.Add(token);
- prev = pos + delimiter.size();
- }
- //Save the last flaots
- token = currLine.substr(prev, pos - prev);
- floats.Add(token);
- }
- //fprintf(stderr, "floats.size = %d", floats.Num());
- //Parse all floats and save them
- for (int i = 0; i < floats.Num(); i += 2) {
- points.Add(FVector((float)::atof(floats[i].c_str()) / 1000.0f, 0.0f, (float)::atof(floats[i + 1].c_str()) / 1000.0f));
- //fprintf(stderr, "%f %f \n", (float)::atof(floats[i].c_str()), (float)::atof(floats[i+1].c_str()));
- }
- return points;
- }
Advertisement
Add Comment
Please, Sign In to add comment