CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2015
    Posts
    500

    Reading a rectangle within a square

    I have a square and our API returrns values from the top left to right.

    Now along with those, I get the X, and Y values of those pixes. Lets say each Pixel is 10cm, I get the centre point of the pixel for each point. As of now I was not interested in the X, and Y, and I was juse storing the value at that pixel continuously from top to bottom ( and let to right) till the end. As I needed to put theese values from left to right from bottom to top.

    But now as per new requirement ,I donot need to store the entire square in the reverse order. But I get a small rectangle which fits in the square. I get the top bottom left corner co-ordinates of this rectangle (which is in yellow). Now from the array which has filled the square , i just have to fill those values which fall in the rectangle in the bottom to top order. (left to right)

    Name:  RectangleInRaster.jpg
Views: 361
Size:  23.5 KB

    Code:
    functionA( std::vector<float>  &pathlossvalues,)
    {
    		while (bContinue)
    			{
    				__int64 xLoss_cm, yLoss_cm;
    				float fSrcLossvAlue;
    				hSrcLoss = pISrcLossIter->GetNextFloat(&xLoss_cm, &yLoss_cm, &fSrcLossvAlue);
    
                                    // Currently just storing the value, But may be i need to store x, and y also
    				pathlossvalues.push_back(fSrcLossvAlue);
    				bContinue = hSrcLoss == S_OK;
    
    				ULONG nMaskedRasterID = 0;
    				ULONG pnRsltMaskedRasterHandle = 0;
    
    
    			}
    }
    Last edited by pdk5; November 1st, 2019 at 02:02 PM.

  2. #2
    Join Date
    May 2015
    Posts
    500

    Re: Reading a rectangle within a square

    And I get the output rectangle top left corner co-ordinates and also the no of pixels on x and y axis

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: Reading a rectangle within a square

    Code:
    void function(ULONG nRendResolution_cm, bool bLimitIteratorRegion, velox::propagation_api::CalculationOutput& out)
    {
    	// Calculate the Ymax_cm and Ymin_cm for the Output raster
    	int Ymax_cm = (out.propagation->getBottomLeftCorner().y)+(out.propagation->getSize().y -1)*nRendResolution_cm + (nRendResolution_cm) / 2;
    
    	int Ymin_cm = (out.propagation->getBottomLeftCorner().y) + (nRendResolution_cm) / 2;
    
    	int Xmin_cm = (out.propagation->getBottomLeftCorner().x) + (nRendResolution_cm) / 2;
    
    			while (bContinue)
    			{
    				__int64 xLoss_cm, yLoss_cm;
    				float fSrcLossvAlue;
    				hSrcLoss = pISrcLossIter->GetNextFloat(&xLoss_cm, &yLoss_cm, &fSrcLossvAlue);
    
    				pathlossvalues.push_back(fSrcLossvAlue);
    				bContinue = hSrcLoss == S_OK;
    
    				ULONG nMaskedRasterID = 0;
    				ULONG pnRsltMaskedRasterHandle = 0;
    				
    				if ((yLoss_cm > Ymin_cm ) && (Ymax) ) && (xLoss_cm == Xmin_cm) )
    				{
    				    bFillInnerRectflag = true;
    				}
    
    				if (bFillInnerRectflag)
    				{
    					out[outindex++] = fSrcLossvAlue[];
    
    					if (innerRectWidthIndex == (innerRectWidth - 1))
    					{
    						bFillInnerRectflag = false;
    					}
    					else
    					{ 
    						innerRectWidthIndex++;
    					}
    				}
    
    				//hrLoss = pSrcRasterLossIter->GetNextFloat(&nX_cm_Loss, &nY_cm_Loss, &fLossVal);
    
    			}
    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading a rectangle within a square

    A nice self-talk...
    Victor Nijegorodov

  5. #5
    Join Date
    May 2015
    Posts
    500

    Re: Reading a rectangle within a square

    Any comments on self talk is helpful

    I wrote the code yesterday for filling the yellow rectangle. Any comments on that will be very helpful.

    Im new to this area and i was initially told to just the reversed array (as asked in the previous Qs). And they suddenly tell me to limit the size of output rectangle also. Ah now i am self talking, found a bug. In the smaller rectangle i need to fill the values from bottom to top. (But as of now since the outer one is read from top to bottom, it looks like i also need to reverse the array). I missed it. Self talk helps. But comments/inputs from others is more helpful indeed

    Right niow i directly write to clients array. Now if i need to reverse i have options :
    1. i need to write into temp array of just size of inner rectangle in straight order and reverse and write to client array later
    2. Change the push back to hold the values of x, y coordinates along with the value as is stored now. And retrive the values for the x,y for the inner rectangle in the reverse order from the whole map of stored values
    Last edited by pdk5; November 2nd, 2019 at 06:47 AM.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading a rectangle within a square

    Quote Originally Posted by pdk5 View Post
    Any comments on self talk is helpful

    I wrote the code yesterday for filling the yellow rectangle. Any comments on that will be very helpful.
    Do you mean someone of us should test your code? Then you have to provide the complete compilable test project.
    But did you already test this code yourself? Does it work? Or are there some problems? What kind of problems?
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Reading a rectangle within a square

    Well the supplied code in post #3 won't actually compile!

    if ((yLoss_cm > Ymin_cm) && (Ymax)) && (xLoss_cm == Xmin_cm) ) - has bracket errors!

    fSrcLossvAlue[] - fSrcLossvAlue is a float!

    const variables such as Ymax_cm et al are not defined as const

    Also you must have a mess of global variables - eg bContinue, bFillInnerRectflag, outindex etc - as these aren't defined in the supplied code!

    NB function is not a good choice of name for a function!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    May 2015
    Posts
    500

    Re: Reading a rectangle within a square

    Thanks a lot kaud. The function i gave is not the actual function in the code. It is legacy code with lot of dependencis. I was just trying to add my pseudocode. Actualy i had to move out this part of the code ( make it independent of other dependencies), for this project.

    Folowing is some more input i did on the code yesterday.

    Code:
    void ModelUseExample::PerformPrediction(ULONG nRendResolution_cm, bool bLimitIteratorRegion, velox::propagation_api::CalculationOutput& out)
    {
    
    	// Calculate the Ymax_cm and Ymin_cm for the Output raster
    	int Ymax_cm = (out.propagation->getBottomLeftCorner().y)+(out.propagation->getSize().y -1)*nRendResolution_cm + (nRendResolution_cm) / 2;
    
    	int Ymin_cm = (out.propagation->getBottomLeftCorner().y) + (nRendResolution_cm) / 2;
    
    	int Xmin_cm = (out.propagation->getBottomLeftCorner().x) + (nRendResolution_cm) / 2;
    	// ===============
    	// Do a prediction
    	// ===============
    
    	printf("Calculating....\n");
    
    	USHORT nResultCode = 0;
    	HRESULT hr = E_FAIL;
    
    	// Two different code paths for models that do an don't support unmasked predictions (just to make things clear)
    //	if (m_pModel->GetSupportStatus(ModelSupports_Unmasked) == S_OK) // Unmasked...
    	{
    		// First create a raster for the resultant prediction:
    		ULONG nUnmaskedRasterHandle = 0;
    
    		//	Also run the interop mdoel, purely so the use of Multi Height storage can be shown
    //		hr = m_pInteropModel->PerformPrediction(&nUnmaskedRasterHandle, &nResultCode);
    //		if (FAILED(hr))
    //		{
    //			printf("Perform Prediction for Interop model failed : %d\n", hr);
    //		}
    
    		hr = m_pModel->PerformPrediction(&nUnmaskedRasterHandle, &nResultCode);
    
    		if (hr == S_OK)
    		{
    			HRESULT hr = m_pRaster->Attach(nUnmaskedRasterHandle);
    			if (FAILED(hr))
    			{
    				printf("m_pRaster->Attach(..) for unmasked raster failed : %d\n", hr);
    				return;
    			}
    
    			//numberOfpathlossvalues, float pathlossvalues, unsigned long widthOfRaster)
    			CComPtr<PredRasterInterfaceLib::IPredRasterIterator2> pISrcLossIter;
    			ULONG nExistingSectionNumber;
    			HRESULT hRes1 = GetWellKnownRasterSectionIterator(PredRasterSectionID_SingleHeight_Loss, m_pRaster, pISrcLossIter, hRes1, nExistingSectionNumber);
    
    			bool bContinue = true;
    			HRESULT hSrcLoss;
    			
    			CComPtr<PredRasterInterfaceLib::IRegion2> pTargetRegion;
    			hr = m_pRaster->GetRegion(nExistingSectionNumber, &pTargetRegion);
    
    			int widthOfRaster = 0;
    
    			pTargetRegion->GetDataWidth(&widthOfRaster);
    
    			boolean bFillInnerRectflag = false;
    			int innerRectWidth = out.propagation->getSize().x;
    			int innerRectWidthIndex = 0;
    			int outindex = 0;
    			float *assetDesignPathlossValues = (float *)malloc(sizeof(float)*(out.propagation->getSize().x) * (out.propagation->getSize().y));
    
    			while (bContinue)
    			{
    				__int64 xLoss_cm, yLoss_cm;
    				float fSrcLossvAlue;
    				hSrcLoss = pISrcLossIter->GetNextFloat(&xLoss_cm, &yLoss_cm, &fSrcLossvAlue);
    
    				//pathlossvalues.push_back(fSrcLossvAlue);
    				bContinue = hSrcLoss == S_OK;
    
    				ULONG nMaskedRasterID = 0;
    				ULONG pnRsltMaskedRasterHandle = 0;
    				
    				if (((yLoss_cm >= Ymin_cm ) && (yLoss_cm <= Ymax_cm))  && (xLoss_cm == Xmin_cm) )
    				{
    				    bFillInnerRectflag = true;
    				}
    
    				if (bFillInnerRectflag)
    				{  
    					assetDesignPathlossValues[outindex++] = fSrcLossvAlue;
    
    					if (innerRectWidthIndex == (innerRectWidth - 1))
    					{
    						bFillInnerRectflag = false;
    						innerRectWidthIndex = 0;
    					}
    					else
    					{ 
    						innerRectWidthIndex++;
    					}
    				}
    
    				//hrLoss = pSrcRasterLossIter->GetNextFloat(&nX_cm_Loss, &nY_cm_Loss, &fLossVal);
    
    			}
    
    			// Fill the output rectangle in reverse order to AssetDesigns output raster
    			size_t numberOfpathlossvalues = (out.propagation->getSize().x) *(out.propagation->getSize().y);
    
    			INT64 index = 0;
    
    			for (long int inindex = numberOfpathlossvalues - widthOfRaster; inindex >= 0; inindex -= widthOfRaster)
    			{
    				for (ULONG j = 0; j < widthOfRaster; ++j)
    				{
    					out.propagation->getBuffer()[index++] = (unsigned short)assetDesignPathlossValues[inindex + j];
    				}
    			}
    
    			// Display the unmasked raster generated ...
    			DisplayRaster(m_pRaster, DrawMode::Unmasked, nUnmaskedRasterHandle, nRendResolution_cm, bLimitIteratorRegion);
    
    			// ===============
    			// Do the masking
    			// ===============
    
    
    	if (hr == S_OK)
    	{
    		printf("Prediction succeeded\n");
    	}
    }
    Last edited by pdk5; November 3rd, 2019 at 07:52 AM.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading a rectangle within a square

    Quote Originally Posted by pdk5 View Post
    ... The function i gave is not the actual function in the code. It is legacy code with lot of dependencis. I was just trying to add my pseudocode.
    Note that if you will still go on posting a "pseudocode" you might receive back the pseudo-responses.
    Victor Nijegorodov

  10. #10
    Join Date
    May 2015
    Posts
    500

    Re: Reading a rectangle within a square

    The code where i had to return the full square in reverse order is as follows, The above code is changed to only contain portion of the square:
    Code:
    void ModelUseExample::PerformPrediction(ULONG nRendResolution_cm, bool bLimitIteratorRegion,  std::vector<float>  &pathlossvalues, int & widthOfRaster)
    {
    	// ===============
    	// Do a prediction
    	// ===============
    
    	printf("Calculating....\n");
    
    	USHORT nResultCode = 0;
    	HRESULT hr = E_FAIL;
    
    	// Two different code paths for models that do an don't support unmasked predictions (just to make things clear)
    //	if (m_pModel->GetSupportStatus(ModelSupports_Unmasked) == S_OK) // Unmasked...
    	{
    		// First create a raster for the resultant prediction:
    		ULONG nUnmaskedRasterHandle = 0;
    
    		//	Also run the interop mdoel, purely so the use of Multi Height storage can be shown
    //		hr = m_pInteropModel->PerformPrediction(&nUnmaskedRasterHandle, &nResultCode);
    //		if (FAILED(hr))
    //		{
    //			printf("Perform Prediction for Interop model failed : %d\n", hr);
    //		}
    
    		hr = m_pModel->PerformPrediction(&nUnmaskedRasterHandle, &nResultCode);
    
    		if (hr == S_OK)
    		{
    			HRESULT hr = m_pRaster->Attach(nUnmaskedRasterHandle);
    			if (FAILED(hr))
    			{
    				printf("m_pRaster->Attach(..) for unmasked raster failed : %d\n", hr);
    				return;
    			}
    
    			//numberOfpathlossvalues, float pathlossvalues, unsigned long widthOfRaster)
    			CComPtr<PredRasterInterfaceLib::PredRasterIterator2> pISrcLossIter;
    			ULONG nExistingSectionNumber;
    			HRESULT hRes1 = GetWellKnownRasterSectionIterator(PredRasterSectionID_SingleHeight_Loss, m_pRaster, pISrcLossIter, hRes1, nExistingSectionNumber);
    
    			bool bContinue = true;
    			HRESULT hSrcLoss;
    			
    			CComPtr<AircomPredRasterInterfaceLib::IRegion2> pTargetRegion;
    			hr = m_pRaster->GetRegion(nExistingSectionNumber, &pTargetRegion);
    
    			pTargetRegion->GetDataWidth(&widthOfRaster);
    
    			while (bContinue)
    			{
    				__int64 xLoss_cm, yLoss_cm;
    				float fSrcLossvAlue;
    				hSrcLoss = pISrcLossIter->GetNextFloat(&xLoss_cm, &yLoss_cm, &fSrcLossvAlue);
    
    				pathlossvalues.push_back(fSrcLossvAlue);
    				bContinue = hSrcLoss == S_OK;
    
    				
    
    			}
    
    			// Display the unmasked raster generated ...
    			DisplayRaster(m_pRaster, DrawMode::Unmasked, nUnmaskedRasterHandle, nRendResolution_cm, bLimitIteratorRegion);
    
    }
    
    And on return , i was reversing the order of the square:
    
    		size_t numberOfpathlossvalues = pathlossvalues.size();
    
    		unsigned short *aDPathlossValues = (unsigned short *)malloc(sizeof(unsigned short)*numberOfpathlossvalues);
    		INT64 outindex = 0;
    
    		for (long int inindex = pathlossvalues.size() - widthOfRaster; inindex >= 0; inindex -= widthOfRaster)
    		{
    			for (ULONG j = 0; j < widthOfRaster; ++j)
    			{
    				aDPathlossValues[outindex++] = (unsigned short) pathlossvalues[inindex + j];
    			}
    		}
    Last edited by pdk5; November 3rd, 2019 at 08:18 AM.

  11. #11
    Join Date
    May 2015
    Posts
    500

    Re: Reading a rectangle within a square

    I have tested partially my previous code (reverse of square). My second requirement was to constrain the output to a rectangle from this square. I am given the size of rectangle in terms of pixels in x and y direction and also the top left corner x and y co-ordinates. I was cming up with the logic and wanted to share here my logic of doing this. And if any better logic ?and inputs. This is part of bigger project and i cannot share all the project as it is huge

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Reading a rectangle within a square

    You don't use malloc() with C++ - and where's the corresponding free() ? You use new and delete - and unless there's a very good reason you use managed pointers not raw pointers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Reading a rectangle within a square

    Quote Originally Posted by pdk5 View Post
    I have tested partially my previous code (reverse of square). My second requirement was to constrain the output to a rectangle from this square. I am given the size of rectangle in terms of pixels in x and y direction and also the top left corner x and y co-ordinates. I was cming up with the logic and wanted to share here my logic of doing this. And if any better logic ?and inputs. This is part of bigger project and i cannot share all the project as it is huge
    Not that this is going to help and maybe it's not possible in this project, but in other projects think about how you can organize the source code in order to reduce dependencies between files and modules.

    That way, you could pull in only a few files into a test project and have what you need for a sample project.

    Of course, the benefits extend beyond being able to have a test project to post to a forum. The real benefit is modular code that can be unit tested and integration tested in small chunks. It helps in debugging to be able to have code that is easily isolatable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured