mardi 3 janvier 2023

How to reconstruct the pixel on a specify cube?

Now I have a vtkImageData (WxHxS) .I want to recontruct the cube to SxHxW

I think there is a mapping for the goal

NEW ORIGIN

(i,0,0) ⇒ (W-i,0,0)

(0,j,0) ⇒ (W,j,0)

(0,0,k) ⇒ (W,0,k)

so I try to setting the new diminsion and extent for the vtkImageData

m->SetDimensions(S, H, W);
m->SetSpacing(origin->GetSpacing()[2], origin->GetSpacing()[1], origin->GetSpacing()[0]);
for (int k = 0; k < W; k++)
    for (int j = 0; j < H; j++)
        for (int i = 0; i< S; i++)
        {
        ptr = (unsigned short *)m->GetScalarPointer(i, j, k);
        ori = (unsigned short *)origin->GetScalarPointer(W - 1 - k, j, S -1- i);
        *ptr = *ori;

            }

//But it doesn't get the matter
//However, when I use the pixel method to copy origin data or upsidedow data, it is correct //like below

//copy the same structure
for (int k = 0; k < S; k++)
    for (int j = 0; j < H; j++)
    for (int i = 0; i< W; i++)
    {
        ori = (unsigned short*)origin->GetScalarPointer(i, j, k);
        ptr = (unsigned short*)m->GetScalarPointer(i, j, k);
        *ptr = *ori;
    }
//upsiaedown
    double val; int i = 0;
    for (vtkIdType f = m->GetNumberOfPoints() - 1; f > -1; f--)
    {
        val =m->GetPointData()->GetScalars()->GetTuple1(f);
        origin->GetPointData()->GetScalars()->SetTuple1(i, val);
        i++;
    }

correct cubic output

Ws2tcpip.h not compiling

I'm making a bandwidth usage monitor using QT creator and every time I include Ws2tcpip.h I get a dozen of compilation errors like : error: redefinition of 'struct ip_mreq'

I only included the header file and still get many errors

#include "bandwidth_usage.h"
#include <windows.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
Bandwidth_Usage::Bandwidth_Usage()
{

}

int Bandwidth_Usage :: get () {

}

lundi 2 janvier 2023

How to generate random or pseudo-random floats? [closed]

I'm trying to make a particle system which uses mainly floating points for position, velocity etc. I need a random number generator to generate random floats for some of those values.

I've tried random(), but its data types are long, so the floats are rounded to 0. This is Arduino (which is on C++11), so it's a bit different from average C++, as in you cannot use <iostream>, <random> etc.

dimanche 1 janvier 2023

#define template types doen't seem to work

suppose the following code:

#define __INIT_TYPE(type) { template<typename type>struct S{ };}

__INIT_TYPE(int);

int main(){
}

the second line produces the following error

Function definition for '\__INIT_TYPE' not found. Expected a declaration.
  1. Why does it happen? so far as I know the macro has to be replaced with the templated struct and which will be declared and then defined.

  2. If I am just missing something and there is a solution to q.1, is it considered a bad practice to nest types in the program with macros like this?

condition_variable::wait_until passes through unexpectedly g++ (9.4.0)

I am trying to understand the behaviour of condition_variable::wait_until. I have two pieces of code which I expect will wait one second and then exit. However, when I execute the programs (compiled with g++ 9.4.0, libc6 2.3.2) the first does not wait at all, whereas the second example does.

(see Update below for how to get correct behaviour in both examples)

Here's the first example which does not wait (unlike what I would expect) when executed:

// foo_passes_thru.cpp
// I compiled with `g++ foo_passes_thru.cpp`
#include <chrono>
#include <mutex>
#include <condition_variable>

std::condition_variable cv;
std::mutex m;

int main(int argc, char * argv[]) {

    using namespace std::chrono_literals;
    
    std::unique_lock<std::mutex> lk(m);
    const auto now = std::chrono::high_resolution_clock::now();
    cv.wait_until(lk, now + 1000ms);

    return 0;

}

And the second example, which, when executed, does wait as expected:

// foo_waits_1sec.cpp
// I compiled with `g++ foo_waits_1sec.cpp -lpthread`
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <thread>

std::condition_variable cv;
std::mutex m;

int main(int argc, char * argv[]) {

    using namespace std::chrono_literals;

    std::thread causes_wait([]{});
    // don't even need the thread to run
    causes_wait.join();

    std::unique_lock<std::mutex> lk(m);
    const auto now = std::chrono::high_resolution_clock::now();
    cv.wait_until(lk, now + 1000ms);

    return 0;

}
  • There does not appear to be a difference in the compiled wait_until.
  • In both programs, there is ultimately a call to pthread_cond_timedwait.

I am not sure if this is a case of unspecified behaviour or there is something incorrect in g++/STL/pthread. The documentation for pthread_cond_timedwait didn't suggest to me that there should be differences in behaviour.

Update

Upon posting, a related question came up that I hadn't yet seen. The behaviour that I expect is restored for the first example if I add the -pthread compiler option. This option should be used to build both.

All indices of a number using recurrion

I am solving a problem in recursion to print all the indices of a number. For example: arr[] = {8,8,10,8,8} and element = 8 then we should have output array as: {0,1,3,4}

I am not getting the output as expected. Here is my code solution:

#include<iostream>
using namespace std;
int allIndexes(int input[], int size, int x, int output[]) {
    if(size==0){
        return 0;
    }
    if(input[0] == x){
        output[0] = 0;    
    }
    int ans = allIndexes(input+1,size-1,x,output);
    for(int i=1;i<ans;i++){
        output[i] = output[i]+1;
    }
    ans = ans+1;
    return ans;
}
int main(){
    int arr[] = {8,8,10,8,8},output[5]={0};
    int size = allIndexes(arr,5,8,output);
    cout<<size<<endl;
    for(int i=0;i<size-1;i++){
        cout<<output[i]<<" ";
    }
    return 0;
}

Why I am getting wrong?

Exception thrown at 0x00007FFF43E0F551 (ucrtbased.dll) [closed]

So I'm trying to make my own game using SDL for the engine and TinyXML for parsing the map. When trying to parse I get a crash which I'm not sure what what is wrong, I just know where it happens and general idea why but I'm not sure how to fix it.

TileSet MapParser::ParseTileset(TiXmlElement* xmlTileset)
{    
    TileSet tileset;       

    //crash    
    tileset.name = xmlTileset->Attribute("name");
    xmlTileset->Attribute("firstgid", &tileset.firstID);
              
    xmlTileset->Attribute("tilecount", &tileset.tileCount);
    tileset.lastID = (tileset.firstID + tileset.tileCount) - 1;
       
    xmlTileset->Attribute("columns", &tileset.colCount);
    tileset.colCount = tileset.tileCount / tileset.colCount;

    xmlTileset->Attribute("tilewidth", &tileset.tileSize);

    TiXmlElement* image = xmlTileset->FirstChildElement();
    tileset.source = image->Attribute("source");
    
    return tileset;
}

I'm trying to assign the name of the XML attribute to the tileset variable that is part of "TileSet" struct

struct TileSet
{
    int firstID, lastID;
    int rowCount, colCount;
    int tileCount, tileSize;
    std::string name, source;
};

It also happens to all assigns to the "TileSet" variables.