HRESULT Set(ULONG ordinal, const DBPARAMBINDINFO &rParamBindInfo) { this->iOrdinal = ordinal; if (this->pwszName != 0) { delete [] this->pwszName; this->pwszName = 0; } if (rParamBindInfo.pwszName != 0) { this->pwszName = new OLECHAR[::wcslen( rParamBindInfo.pwszName ) + 1]; if (this->pwszName == 0) return E_OUTOFMEMORY; ::wcscpy( this->pwszName, rParamBindInfo.pwszName ); } ATLASSERT(rParamBindInfo.pwszDataSourceType); this->wType = CheckDataType(rParamBindInfo.pwszDataSourceType); this->ulParamSize = rParamBindInfo.ulParamSize; this->dwFlags = rParamBindInfo.dwFlags; this->bPrecision = rParamBindInfo.bPrecision; this->bScale = rParamBindInfo.bScale; return S_OK; }
//------------------------------------------------------------------------------ bool NonlinearConstraint::Initialize() { #if DEBUG_NONLINEAR_CONSTRAINT_INIT MessageInterface::ShowMessage (wxT("NonlinearConstraint::Initialize() entered. interpreted=%d, optimizer=%p, ") wxT("arg1=%p, arg2=%p\n"), interpreted, optimizer, arg1, arg2); #endif // Commented out since this caused crash when this command created // from the GUI and run (loj: 2007.05.15) //if (!interpreted) // if (!InterpretAction()) // throw CommandException( // "NonlinearConstraint: error interpreting input data\n"); bool retval = GmatCommand::Initialize(); if (optimizer == NULL) throw CommandException( wxT("Optimizer not initialized for NonlinearConstraint command\n \"") + generatingString + wxT("\"\n")); // Set references for the wrappers #ifdef DEBUG_NONLINEAR_CONSTRAINT_PARAM MessageInterface::ShowMessage(wxT("Setting refs for arg1\n")); #endif if (SetWrapperReferences(*arg1) == false) return false; CheckDataType(arg1, Gmat::REAL_TYPE, wxT("NonlinearConstraint")); #ifdef DEBUG_NONLINEAR_CONSTRAINT_PARAM MessageInterface::ShowMessage(wxT("Setting refs for arg2\n")); #endif if (SetWrapperReferences(*arg2) == false) return false; CheckDataType(arg2, Gmat::REAL_TYPE, wxT("NonlinearConstraint")); // The optimizer cannot be finalized until all of the loop is initialized optimizerDataFinalized = false; #if DEBUG_NONLINEAR_CONSTRAINT_INIT MessageInterface::ShowMessage (wxT("NonlinearConstraint::Initialize() exiting. optimizer=%p, arg1=%p, arg2 = %p\n"), optimizer, arg1, arg2); #endif return retval; }
//------------------------------------------------------------------------------ bool Minimize::Initialize() { #if DEBUG_MINIMIZE_INIT // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ debug ~~~~ MessageInterface::ShowMessage ("Minimize::Initialize() entered. optimizer=%p, objective=%p\n", optimizer, objective); #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end debug ~~~~ if (!interpreted) if (!InterpretAction()) throw CommandException( "Minimize: error interpreting input data\n"); bool retval = SolverSequenceCommand::Initialize(); if (optimizer == NULL) throw CommandException( "Optimizer not initialized for Minimize command\n \"" + generatingString + "\"\n"); //Integer id = optimizer->GetParameterID("Goals"); // no clue //optimizer->SetStringParameter(id, goalName); // find objectiveName //GmatBase *obj = (*objectMap)[objectiveName]; //if (obj == NULL) //{ // std::string errorstr = "Could not find variable parameter "; // errorstr += objectiveName; // throw CommandException(errorstr); //} // The optimizer cannot be finalized until all of the loop is initialized optimizerDataFinalized = false; if (SetWrapperReferences(*objective) == false) return false; CheckDataType(objective, Gmat::REAL_TYPE, "Minimize"); #if DEBUG_MINIMIZE_INIT // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ debug ~~~~ MessageInterface::ShowMessage ("Minimize::Initialize() exiting. optimizer=%p, objective=%p\n", optimizer, objective); #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end debug ~~~~ return retval; }
bool Minimize::SetElementWrapper(ElementWrapper *toWrapper, const std::string &withName) { bool retval = false; if (toWrapper == NULL) return false; if (toWrapper->GetWrapperType() == Gmat::ARRAY_WT) { throw CommandException("A value of type \"Array\" on command \"" + typeName + "\" is not an allowed value.\nThe allowed values are:" " [ Variable, Array Element, or Parameter ]. "); } if (toWrapper->GetWrapperType() == Gmat::NUMBER_WT) { throw CommandException("A value of type \"Number\" on command \"" + typeName + "\" is not an allowed value.\nThe allowed values are:" " [ Variable, Array Element, or Parameter ]. "); } //if (toWrapper->GetWrapperType() == Gmat::STRING_OBJECT_WT) //{ // throw CommandException("A value of type \"String Object\" on command \"" + typeName + // "\" is not an allowed value.\nThe allowed values are:" // " [ Real Number, Variable, Array Element, or Parameter ]. "); //} #ifdef DEBUG_WRAPPER_CODE MessageInterface::ShowMessage(" Setting wrapper \"%s\" on Minimize command\n", withName.c_str()); #endif CheckDataType(toWrapper, Gmat::REAL_TYPE, "Minimize", true); if (objectiveName == withName) { objective = toWrapper; // Reset the generating string so initialization works correctly GetGeneratingString(Gmat::SCRIPTING, "", ""); retval = true; } return retval; }
//------------------------------------------------------------------------------ // bool SetElementWrapper(ElementWrapper *toWrapper, const wxString &withName) //------------------------------------------------------------------------------ bool NonlinearConstraint::SetElementWrapper(ElementWrapper *toWrapper, const wxString &withName) { bool retval = false; if (toWrapper == NULL) return false; if (toWrapper->GetWrapperType() == Gmat::ARRAY_WT) { throw CommandException(wxT("A value of type \"Array\" on command \"") + typeName + wxT("\" is not currently an allowed value.\nThe allowed values are:") wxT(" [ Real Number, Variable, Array Element, or Parameter ]. ")); } //if (toWrapper->GetWrapperType() == Gmat::STRING_OBJECT_WT) //{ // throw CommandException("A value of type \"String Object\" on command \"" + typeName + // "\" is not an allowed value.\nThe allowed values are:" // " [ Real Number, Variable, Array Element, or Parameter ]. "); //} CheckDataType(toWrapper,Gmat::REAL_TYPE, wxT("NonlinearConstraint"), true); #ifdef DEBUG_NLC_WRAPPER_CODE MessageInterface::ShowMessage(wxT(" Setting wrapper \"%s\" on NLC command\n"), withName.c_str()); #endif if (arg1Name == withName) { arg1 = toWrapper; retval = true; } if (arg2Name == withName) { arg2 = toWrapper; retval = true; } return retval; }
bool KGDAL2CV::ImgWriteByGDAL(GDALRasterBand* pBand, const cv::Mat img, int xStart, int yStart) { // if dataset is null, then there was a problem if (pBand == nullptr){ return false; } // make sure we have the proper access if (pBand->GetAccess() == GA_ReadOnly){ std::cout << "Invalid access type of the GDALRasterBand!" << std::endl; return false; } cv::Mat imgToSave = img.clone(); if (img.channels() > 1){ std::cout << "More channels of the cv::Mat will be passed!" << std::endl; std::vector<cv::Mat> singleMats; cv::split(img, singleMats); //singleMats[0].copyTo(imgToSave); imgToSave = singleMats[0].clone(); } int width = pBand->GetXSize(); int height = pBand->GetYSize(); if (xStart < 0 || yStart < 0 || xStart >= width || yStart >= height) { std::cout << "wrong param!" << std::endl; return false; } int xWidth = imgToSave.cols; int yWidth = imgToSave.rows; if (xStart + xWidth > width) { std::cout << "Saved image will be cutted!" << std::endl; imgToSave = (imgToSave.colRange(0, width - xStart)).clone(); } if (yStart + yWidth > height) { std::cout << "Saved image will be cutted!" << std::endl; imgToSave = (imgToSave.rowRange(0, height - yStart)).clone(); } GDALDataType dataType = pBand->GetRasterDataType(); CheckDataType(dataType, imgToSave); xWidth = imgToSave.cols; yWidth = imgToSave.rows; int cvDepth = imgToSave.depth(); imgToSave.reshape(1, 1); double *imgBuff = new(std::nothrow) double[xWidth * yWidth]; if (nullptr == imgBuff) return false; if (cvDepth == CV_8S) { char * data = imgToSave.ptr<char>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } else if (cvDepth == CV_16U) { unsigned short * data = imgToSave.ptr<unsigned short>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } else if (cvDepth == CV_16S) { short * data = imgToSave.ptr<short>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } else if (cvDepth == CV_32S) { int * data = imgToSave.ptr<int>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } else if (cvDepth == CV_32F) { float * data = imgToSave.ptr<float>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } else if (cvDepth == CV_64F) { double * data = imgToSave.ptr<double>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } else{ unsigned char * data = imgToSave.ptr<unsigned char>(0); for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = static_cast<double>(data[index]); } // datatype translate //for (int index = 0; index < xWidth * yWidth; ++index) imgBuff[index] = range_cast_inv(dataType, cvDepth, imgBuff[index]); pBand->RasterIO(GF_Write, xStart, yStart, xWidth, yWidth, imgBuff, xWidth, yWidth, GDT_Float64, 0, 0); pBand->FlushCache(); delete[] imgBuff; return true; }
// be sure the cv::Mat either a gray image or in RGB order! bool KGDAL2CV::ImgWriteByGDAL(GDALDataset * dataset, const cv::Mat img, int xStart, int yStart) { // if dataset is null, then there was a problem if (dataset == nullptr){ return false; } // make sure we have pixel data inside the raster if (dataset->GetRasterCount() <= 0){ return false; } // make sure we have the proper access if (dataset->GetAccess() == GA_ReadOnly){ std::cout << "Invalid access type of the dataset!" << std::endl; return false; } int nBand = dataset->GetRasterCount(); if (nBand > img.channels()) { std::cout << "The channels of GDALDataset shouldn't be more than cv::Mat!" << std::endl; return false; } int width = dataset->GetRasterXSize(); int height = dataset->GetRasterYSize(); if (xStart < 0 || yStart < 0 || xStart >= width || yStart >= height) { std::cout << "wrong param!" << std::endl; return false; } cv::Mat imgToSave = img; int xWidth = imgToSave.cols; int yWidth = imgToSave.rows; if (xStart + xWidth > width) { std::cout << "Saved image will be cutted!" << std::endl; imgToSave = imgToSave.colRange(0, width - xStart); } if (yStart + yWidth > height) { std::cout << "Saved image will be cutted!" << std::endl; imgToSave = imgToSave.rowRange(0, height - yStart); } /*if (width != img.cols || height != img.rows) { std::cout << "The size of cv::Mat and GDALDataset should be same!" << std::endl; return false; }*/ GDALDataType dataType = dataset->GetRasterBand(1)->GetRasterDataType(); CheckDataType(dataType, imgToSave); std::vector<cv::Mat> singleMats; int ret = 0; cv::split(imgToSave, singleMats); for (int index = 0; index < nBand; ++index) { GDALRasterBand* band = dataset->GetRasterBand(index + 1); ret += (true == ImgWriteByGDAL(band, singleMats[index], xStart, yStart) ? 0 : 1); } return (0 == ret); }
//------------------------------------------------------------------------------------------------------------------------------------ // BOOL ItemCommandLoop( LPRefObj pRefSrc, ULONG ulItmID ) { ULONG ulDataType = 0; LPRefObj pRefItm = NULL; char buf[256]; UWORD wSel; BOOL bRet = true; pRefItm = GetRefChildPtr_ID( pRefSrc, ulItmID ); if ( pRefItm == NULL ) { // Create Item object and RefSrc structure. if ( AddChild( pRefSrc, ulItmID ) == true ) { printf("Item object is opened.\n"); } else { printf("Item object can't be opened.\n"); return false; } pRefItm = GetRefChildPtr_ID( pRefSrc, ulItmID ); } // command loop do { printf( "\nSelect (1-7, 0)\n" ); printf( " 1. Select Data Object 2. Delete 3. IsAlive\n" ); printf( " 4. Name 5. DataTypes 6. DateTime\n" ); printf( " 7. StoredBytes\n" ); printf( " 0. Exit\n>" ); scanf( "%s", buf ); wSel = atoi( buf ); switch( wSel ) { case 1:// Show Children // Select Data Object ulDataType = 0; bRet = SelectData( pRefItm, &ulDataType ); if ( bRet == false ) return false; if ( ulDataType == kNkMAIDDataObjType_Image ) { // reset file removed flag g_bFileRemoved = false; bRet = ImageCommandLoop( pRefItm, ulDataType ); // If the image data was stored in DRAM, the item has been removed after reading image. if ( g_bFileRemoved ) { RemoveChild( pRefSrc, ulItmID ); pRefItm = NULL; } } else if ( ulDataType == kNkMAIDDataObjType_Video ) { // reset file removed flag g_bFileRemoved = false; bRet = MovieCommandLoop( pRefItm, ulDataType ); if ( g_bFileRemoved ) { RemoveChild( pRefSrc, ulItmID ); pRefItm = NULL; } } else if ( ulDataType == kNkMAIDDataObjType_Thumbnail ) { bRet = ThumbnailCommandLoop( pRefItm, ulDataType ); } if ( bRet == false ) return false; break; case 2:// Delete ulDataType = 0; bRet = CheckDataType( pRefItm, &ulDataType ); if ( bRet == false ) { puts( "Movie object is not supported.\n" ); break; } bRet = DeleteDramCapability( pRefItm, ulItmID ); if ( g_bFileRemoved ) { // If Delete was succeed, Item object must be closed. RemoveChild( pRefSrc, ulItmID ); pRefItm = NULL; } break; case 3:// IsAlive bRet = SetBoolCapability( pRefItm, kNkMAIDCapability_IsAlive ); break; case 4:// Name bRet = SetStringCapability( pRefItm, kNkMAIDCapability_Name ); break; case 5:// DataTypes bRet = SetUnsignedCapability( pRefItm, kNkMAIDCapability_DataTypes ); break; case 6:// DateTime bRet = SetDateTimeCapability( pRefItm, kNkMAIDCapability_DateTime ); break; case 7:// StoredBytes bRet = SetUnsignedCapability( pRefItm, kNkMAIDCapability_StoredBytes ); break; default: wSel = 0; } if ( bRet == false ) { printf( "An Error occured. Enter '0' to exit.\n>" ); scanf( "%s", buf ); bRet = true; } } while( wSel > 0 && pRefItm != NULL ); if ( pRefItm != NULL ) { // If the item object remains, close it and remove from parent link. bRet = RemoveChild( pRefSrc, ulItmID ); } return bRet; }
STDMETHOD(SetParameterInfo)(ULONG cParams, const ULONG rgParamOrdinals[], const DBPARAMBINDINFO rgParamBindInfo[]) { HRESULT hr; if (cParams == 0) { m_paramInfo.RemoveAll(); m_nSetParams = 0; return S_OK; } if (cParams > 0 && rgParamOrdinals == 0) return E_INVALIDARG; if (rgParamBindInfo != 0) { bool allParamNamesSet = true, oneParamNameSet = false; for (ULONG i = 0; i < cParams; i++) { // We do not handle default parameter conversion if(rgParamBindInfo[i].pwszDataSourceType == 0) return E_INVALIDARG; DBTYPE dbType; if((dbType = CheckDataType(rgParamBindInfo[i].pwszDataSourceType)) == DBTYPE_EMPTY) return DB_E_BADTYPENAME; if( rgParamBindInfo[i].pwszName == 0 ) allParamNamesSet = false; else oneParamNameSet = true; if( rgParamBindInfo[i].dwFlags & ~(DBPARAMIO_INPUT | DBPARAMIO_OUTPUT) ) return E_INVALIDARG; } if( oneParamNameSet && ! allParamNamesSet ) return DB_E_BADPARAMETERNAME; } for (ULONG i = 0; i < cParams; i++) { // Look for the parameter already in the list for (int j = m_nSetParams; --j >= 0;) { if( rgParamOrdinals[i] == m_paramInfo[j].iOrdinal ) break; } if (j >= 0) { // Discard the type info. for this parameter if( rgParamBindInfo == 0 ) { m_paramInfo[j].~ParamInfo(); while( j < (int)m_nSetParams ) { m_paramInfo[j] = m_paramInfo[j + 1]; j++; } --m_nSetParams; } // Change parameter type info??? else { } } else if (rgParamBindInfo != 0) { ParamInfo tempInfo; m_paramInfo.SetAtGrow( m_nSetParams, tempInfo ); ParamInfo ¶mInfo = m_paramInfo[m_nSetParams]; if (FAILED(hr = paramInfo.Set( rgParamOrdinals[i], rgParamBindInfo[i]))) return hr; m_nSetParams++; } } return S_OK; }